Presentation is loading. Please wait.

Presentation is loading. Please wait.

THREADS.

Similar presentations


Presentation on theme: "THREADS."— Presentation transcript:

1 THREADS

2 Intro A “Thread” represents a separate path of execution of a group of statements. In a Java program, if we write a group of statements, then these statements are executed by JVM' one by one. This execution is called a thread, because JVM uses a thread to execute these statements. This means that in every Java program, there is always a thread running internally.

3 A thread is a single sequential flow of control within a program
A thread is a single sequential flow of control within a program. A thread often completes a specific task. By default, a Java application uses a single thread, called the main thread. However, some programs can benefit by using two or more threads to allow different parts of the program to executes simultaneously. This thread is used by JVM to execute the program statements. What is this thread? Let us write a program to see what that thread is:

4 There are two ways to create a thread: 1. By extending Thread class
How to create thread: There are two ways to create a thread: 1. By extending Thread class 2. By implementing Runnable Interface. Thread class: Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. Commonly used Constructors of Thread class: Thread() ; Thread(String name); Thread(Runnable r) Thread(Runnable r,String name)

5 Runnable interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). public void run(): is used to perform action for a thread.

6 Starting a thread: start() method of Thread class is used to start a newly created thread. It performs following tasks: A new thread starts (with new callstack). The thread moves from New state to the Runnable state. When the thread gets a chance to execute, its target run() method will run.

7 Life Cycle of Thread

8

9

10 Who makes your class object as thread object?
Thread class constructor allocates a new thread object. When you create object of multi class, your class constructor is invoked (provided by compiler ) from where Thread class constructor is invoked (by super() as first statement). So your Multi class object is thread object now.

11

12 So you need to explicitly create Thread class object.
If you are not extending the Thread class, your class object would not be treated as a thread object. So you need to explicitly create Thread class object. We are passing object of your class that implements Runnable so that your class run() method may execute.

13

14

15 Result can differ at different time

16

17 In the preceding program, currentThread() is a static method in Thread class. So we called it as Thread.currentThread().

18 Then this method gave an object ‘t’ of Thread class
Then this method gave an object ‘t’ of Thread class. When we displayed this object t, it displayed its contents as: Thread [main,5,main] Here, Thread indicates that ‘t’ is a Thread class object. And the first main indicates the name of the thread running the current code. We get 5 which is a number representing the priority of the thread. Every thread will have a priority number associated with it. These priority numbers will range from 1 to is the minimum priority, and 10 is the maximum priority of a thread.

19 If a thread is t, its name can be known using t.getName () method.
If the priority number of a thread is more, it is given more preference while execution by JVM. The default priority number of a thread is 5. The next “main” indicates the thread group name to which this thread belongs. A thread group represents a group of threads as a single unit. The 'main' thread belongs to 'main' thread group. See the last line in the output of the program. We are displaying the currently running thread's name which is 'main'. If a thread is t, its name can be known using t.getName () method.

20 What does the preceding program indicate?
When we write any program in Java, JVM internally uses a thread called 'main thread' to run the statements of the program. This thread is responsible for executing our statements and displaying the results.

21 Which thread always runs in a Java program by default? main thread.
A thread represents execution of statements. The way the statements are executed is of two types: 1) Single tasking 2) Multi asking.

22 Single Tasking A task means doing some calculation, processing, etc.
Generally, a task involves execution of a group of statements, for example executing a program. In 'single tasking' environment, only one task is given to the processor at a time. See Figure.

23

24 Here, let us take an example of a student who goes to the lab to write some programs. He types his first program. It may be taking some 10 minutes. When he is typing his program, the micro processor is sitting idle, without any work. After typing is completed, he gives the program to the processor. The processor executes it within a millisecond. Since now-a-days processors can execute millions of instructions per second, even if the student has written a program of 100 lines, the processor will not take more than a fraction of a second to complete its execution. After verifying the results, the student has started typing the second program. It may take another 10 or 15 minutes. Within this time, the processor sits idle, waiting for the job. When the student submits the second program, then it works for a fraction of a second on the program. Then again it goes into idle state. From this discussion, we can understand that the processor is sitting idle without any work for most of the time.

25 In single tasking, only one task is given to the processor at a time.
This means we are wasting a lot of processor time and microprocessor has to sit idle without any job for a long time. This is the drawback in single tasking.

26 Multi Tasking To use the processor's time in an optimum way, we can give it several jobs at a time. This is called multi tasking. But how can we give several jobs at a time? Suppose there are 4 tasks that we want to execute. We load them into the memory, as shown in next Figure. The memory is divided into 4 parts and the jobs are loaded there. Now, the micro processor has to execute them all at a time. So the processor will take small time duration, like a millisecond and divide this time between the number of jobs. Here, 4 jobs are there.

27 So we get 1/4 millisecond time for each job
So we get 1/4 millisecond time for each job. This small part of the processor time is called 'time slice'. It will allot exactly 1/4 millisecond time for executing each of the jobs. Within this time slice, it will try to execute each job. Suppose, it started at first job, it will spend exactly % millisecond time executing the first job. Within this time duration, if it could not complete the first job, then what it does? In that case, it stores the intermediate results till then it obtained in a temporary memory, and then it goes to the second task. It then spends exactly ¼ millisecond time executing the second task. Within this time, if it can complete this task, no problem. Suppose it could not complete this task, then it goes to the third task, storing the results in a temporary memory. Similarly, it will spend exactly 1/4 millisecond for third task, and another ¼ millisecond for the fourth task. After executing the fourth task, it will come back to the first task, in a circular manner. This is called 'round robin' method.

28 The processor, after returning to the first task, again starts execution from the point, where it has left that task earlier. It will execute the first task exactly for ¼ millisecond this time, and proceeds for the second task and then third and fourth before coming back to the first task in a round robin way. So if you have submitted the first job, you can understand that the processor is executing your job for ¼ millisecond and then keeping you waiting for another ¼ millisecond, while it is going and executing the other tasks. After ¾ millisecond, it is again coming back to your job and executing your job for another ¼ . millisecond time. But you will not be aware that you are kept waiting for ¾ millisecond time, as this time is very small. You will feel that the processor is spending its time executing your job only. Similarly, the second person who submitted the second job will also feel that only his job is being executed by the processor. The third and fourth persons will also feel the same way. It is something like all the 4 jobs are executed by the processor simultaneously. This is called multi tasking.

29

30 Generally, we have only one processor in our Computer systems
Generally, we have only one processor in our Computer systems. One processor has to execute several tasks means that it has to execute them in a round robin method. Strictly speaking, this is not multi tasking, since the processor is quickly executing the tasks one by one, so quickly that we feel all the jobs are executed by the processor at a time. Multitasking cannot be a real phenomenon with single processor systems. If we want to really achieve multi tasking, we need Computers with multiple processors.

31 We are engaging most of the processor time and it is not sitting idle.
The main advantage of multi tasking is to use the processor time in a better way. We are engaging most of the processor time and it is not sitting idle. In this way, we can complete several tasks at a time, and thus achieve good performance.

32 Multi tasking is of two types: a) Process-based multi tasking b) Thread-based multi tasking.
So far, we discussed the first type, i.e. Process-based multi tasking. Now let us think about Thread-based multi tasking. In Process-based multi tasking, several programs are executed at a time, by the microprocessor. In Thread-based multi tasking, several parts of the same program is executed at a time, by the microprocessor.

33 So the processor uses 2 separate threads to execute these two parts.
See next Figure. Here, we have a program. In this program, there are 2 parts. These parts may represent two separate blocks of code or two separate methods containing code. Each part may perform a separate task. The processor should execute the two parts (tasks) simultaneously. So the processor uses 2 separate threads to execute these two parts.

34

35 We can imagine these threads as hands of the microprocessor.
Each thread can be imagined as an individual process that can execute a separate set of statements. We can imagine these threads as hands of the microprocessor. We have 2 hands, so we can do 2 things at a time. Similarly, if a processor has 2 threads, it can do 2 tasks at a time. This is called Thread-based multitasking. Achieving multi tasking with threads has one advantage ,i.e. since the threads are light weight processes, they use minimum resources of the system.

36 Why threads are called light-weight?
Threads are light-weight because they utilize minimum resources of the system. This means they take less memory and less processor time. What is the difference between single tasking and multi tasking? Executing only one job at a time is called single tasking. Executing several jobs at a time is called multi tasking. In single tasking, the processor time is wasted, but in multi tasking, we can utilize the processor time in an optimum way.

37 Life Cycle of Thread

38 Uses of Threads Threads can be used for multiple purposes. Some of the uses of threads are: Threads are mainly used in server-side programs to serve the needs of multiple clients on a network or Internet. On Internet, a server machine has to cater the needs of thousands of clients, at a time. For this purpose, if we use threads in the server, they can do various jobs at a time, thus they can handle several clients.

39 Uses of Threads Contd. Threads are also used to create games and animation. Animation means moving the objects from one place to another. In many games, generally we have to perform more than one task simultaneously. There, threads will be of invaluable help. For example, in a game, a flight may be moving from left to right. A machine gun should shoot it, releasing the bullets at the flight. These 2 tasks should happen simultaneously. For this purpose, we can use 2 threads, one thread will move the flight and the other one will move the bullet, simultaneously towards the flight.

40 Creating a Thread and Running it
We know that in every Java program, there is a main thread available already. Apart from this main thread, we can also create our own threads in a program. The following steps should be used:

41 class MyClass extends Thread or class MyClass implements Runnable
Create a class that extends Thread class or implements Runnable interface. Both the Thread class and Runnable interface are found in java.lang package. class MyClass extends Thread or class MyClass implements Runnable Now in this class, write a run () method as: public void run() { statements; }

42 By default, this run () method is recognized and executed by a thread.
Create an object to Myclass, so that the run () method is available for execution. MyClass obj = new MyClass(); Now, create a thread and attach the thread to the object obj. Thread t = new Thread(obj); Or Thread t = new Thread(obj,”threadname “); Run the thread. For this purpose, we should use start () method of Thread class. t.start();

43

44

45

46

47

48

49 Commonly used methods of Thread class
1. public void run(): is used to perform action for a thread. 2. public void start(): starts the execution of the thread. JVM calls the run() method on the thread. 3. public void sleep(long milliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. 4. public void join(): waits for a thread to die. 5. public void join(long milliseconds): waits for a thread to die for the specified milliseconds. 6. public int getPriority(): returns the priority of the thread. 7. public int setPriority(int priority): changes the priority of the thread.

50 8. public String qetName(): returns the name of the thread. 9
8. public String qetName(): returns the name of the thread. 9. public void setName(String name): changes the name of the thread. 10. public Thread currentThread(): returns the reference of currently executing thread. 11. public int getId(): returns the id of the thread. 12. publIc Thread.State getState(): returns the state of the thread. 13. public boolean IsAlive(): tests If the thread is alive. 14. public void yield() : causes the currently executing thread object to temporally pause and allow other threads to execute.

51 15. public void suspend(): is used to suspend the thread (deprecated )
15. public void suspend(): is used to suspend the thread (deprecated ) public void resume(): is used to resume the suspended thread ( deprecated ) public void stop(): is used to stop the thread (deprecated) public boolean isDaemon(): tests if the thread is a daemon thread publIc void setDaemon(boolean b): marks the thread as daemon or user thread public void interrupt(): interrupts the thread public boolean isInterrupted() : tests if the thread has been interrupted public static boolean interrupted() : tests if the current thread has been interrupted.

52

53

54

55

56 When we run the thread, it runs the run () method of MyThread object.
In this program, we create MyThread class with run() method and then attach a thread to this MyThread class object. When we run the thread, it runs the run () method of MyThread object.  Here, the class MyThread extends Thread class. We can also replace this statement with a statement like: class MyThread implements Runnable. Thread class and Runnable interface both have public void run() method in them. By writing class MyThread extends Thread, we are overriding the run() method of Thread class. By writing cIass MyThread implements Runnable interface, we are implementing the run() method of the Runnable interface. In Demo class, we created an object to MyThread class. This means that object contains run() method. Then we attached this object to thread t, as: Thread t = new Thread(obj);

57 If we give t.start(), then the thread ‘t’, starts running the code inside the run() method of the object obj. In the run() method, we wrote code to print numbers from 1 to using a for loop. If you want to terminate the program in the middle, you can press Ctrl+C from the keyboard. This leads to abnormal program termination. It means the entire program is terminated, not just the thread.

58 If we press Ctrl+C, we are abnormally terminating the program.
 If we want to terminate only the thread that is running the code inside run() method, we should devise our own mechanism. If we press Ctrl+C, we are abnormally terminating the program. This is dangerous. Abnormal program termination may cause loss of data and lead to unreliable results. So we should terminate the thread only, not the program. How can we terminate the thread smoothly is the question now.

59

60

61

62 Terminating the Thread
A thread will terminate automatically when it comes out of run() method. To terminate the thread on our own, we have to device our own logic. For this purpose, the following steps can be used: Create a boolean type variable and initialize it to false. boolean stop = false; Let us assume that we want to terminate the thread when the user presses <Enter> key. So, when the user presses that button, make the boolean type variable as true stop = true;

63 if(stop == true) return; }
Check this variable in run() method and when it is true, make the thread return from the run() method. public void run() { if(stop == true) return; }

64 How can you stop a thread in Java?
First of all, we should create a boolean type variable which stores 'false'. When the user wants to stop the thread, we should store 'true' into the variable. The status of the variable is checked in the run() method and if it is true, the thread executes 'return' statement and then stops.

65

66

67 Sleeping a thread (sleep() method)
The sleep() method of Thread class is used to sleep a thread for the specified time. Syntax: public static void sleep(long miliseconds) throws InterruptedException

68

69

70 Can we start a thread twice?
No. After starting a thread, it can never be started again. If you does so, an llegalThreadStateException is thrown.

71

72

73

74 What if we call run() method directly instead of start() method
Each thread starts in a separate call stack. Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.

75

76

77

78 It will always show same output.

79

80

81

82

83

84 getName(),setName(String) and getId() method:
public String getName() public void setName(String name) public long getId()

85

86

87 The currentThread() method:
The currentThread() method returns a reference to the currently executing thread object. Syntax: public static Thread currentThread()

88

89 Naming Thread The Thread class provides methods to change and get the name of a thread. By default, each thread has a name i.e. thread-0, thread-1 and so on. We can change the name of the thread by using setName() method. The syntax of setName() and getName() methods are given below: public String getName(): is used to return the name of a thread. public void setName(String name): is used to change the name of a thread.

90

91

92 The Thread Scheduler: The thread scheduler is the part of the JVM that decides which thread should run. There is no guarantee that which runnable thread will be chosen to run by the thread scheduler. Only one thread at a time can run in a single process. The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

93 What is the difference between preemptive scheduling and time slicing?
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

94 Single Tasking Using a Thread
A thread can be employed to execute one task at a time. Suppose there are 3 tasks to be executed. We can create a thread and pass the 3 tasks one by one to the thread. For this purpose, we can write all these tasks separately in separate methods: task1(), task2(), task3(). Then these methods should be called from run() method, one by one. Remember, a thread executes only the code inside the run() method. It can never execute other methods unless they are called from run() .

95

96

97 Multi Tasking Using Threads
In multi tasking, several tasks are executed at a time. For this purpose, we need more than one thread. For example, to perform 2 tasks, we can take 2 threads and attach them to the 2 tasks. Then those tasks are simultaneously executed by the two threads. Using more than one thread is called 'multi threading'.

98 Example of cinema-hall
When we go to a movie theatre, generally a person is there at the door-checking and cutting the tickets. When we enter the hall, there is another person who shows the seats to us. Suppose there is only one person (1 thread) doing these two tasks. He has to first cut the ticket and then come along with us to show the seat. Then he goes back to the door to cut the second ticket and then again enter the hall to show the seat for the second ticket. Like this, if he is does the things one by one, it takes a lot of time, and even though the show is over, there will be still a few people left outside the door waiting to enter the hall! This is pretty well known to the theatre management. So what they do? They employ two persons (2 threads) for this purpose. The first person will cut the ticket, and the second one will show the seat. When the second person is showing the seat, the first person cuts the second ticket. Like this, both the persons can act simultaneously and hence there will be no wastage of time. This is shown in Program.

99

100 Next slide same program without try catch block ..

101

102 Program output with out try catch block

103 The join() method The join() method waits for a thread to die.
In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. Syntax public void join() throws InterruptedException public void join(long milliseconds) throws InterruptedException

104 When t1 completes its task then t2 and t3 starts executing

105 When t1 completes its task then t2 and t3 starts executing

106 Multiple Threads Acting on Single Object
In theatre example, we have used 2 threads on the 2 objects of MyThread class. It is also possible to use 2 or more threads on a single object. But in this case, sometimes we get unreliable results. First let us see why 2 threads should share the same object (same run() method). We write an object to represent one task. If there is a different task, we take another object. When two people (threads) perform same task, then they need same object (run() method) to be executed each time.

107 Let us see now see to whom that berth is allotted.
Take the case of railway reservation. Every day several people want reservation of a berth for them. The procedure to reserve the berth is same for all the people. So we need same object with same run () method to be executed repeatedly for all the people (threads). Let us think that only one berth is available in a train, and two passengers (threads) are asking for that berth. In reservation counter no. 1 , the clerk has sent a request to the server to allot that berth to his passenger. In counter no.2, the second clerk has also sent a request to the server to allot that berth to his passenger. Let us see now see to whom that berth is allotted.

108

109

110

111 When the delay is 0.

112 What is the solution for this problem?
Let us keep the second thread t2 wait till the first thread t1 completes and comes out. Let us not allow any other thread to enter the object till t1 comes out. This means we are preventing the threads to act on the same object simultaneously. This is called Thread Synchronization or Thread safe. See Figure.

113

114 What is Thread synchronization?
When a thread is already acting on an object, preventing any other thread from acting on the same object is called Thread synchronization' or ‘Thread safe' . The object on which the threads are synchronized is called 'synchronized object'. Thread synchronization is recommended when multiple threads are used on the same object (in multithreading).

115 Synchronized object is like a locked object, locked on a thread.
It is like a room with only one door. A person has entered the room and locked from it from behind. The second person who wants to enter the room should wait till the first person comes out. In this way, a thread also locks the object after entering it. Then the next thread cannot enter it till the first thread comes out. This means the object is locked mutually on threads. So, this object is called 'mutex' (mutually exclusive lock).

116 How can we synchronize the object?
There are two ways of doing this. Using synchronized block: Here, we can embed a group of statements of the object (inside run() method) within a synchronized block, as shown here: synchronized(object) { Statements; } Here, object represents the object to be locked or synchronized. The statements inside the synchronized block are all available to only one thread at a time. They are not available to more than one thread simultaneously.

117 synchronized void display() { statements; }
Another way using synchronized keyword: We can synchronize an entire method by using synchronized keyword. For example, if we want to synchronize the code of display() method, then add the synchronized keyword before the method name as shown here: synchronized void display() { statements; } Now the statements inside the display() method are not available to more than one thread at a time. This method code is synchronized.

118 Synchronized block is useful to synchronize a block of statements.
What is the difference between synchronized block and synchronized keyword? Synchronized block is useful to synchronize a block of statements. Synchronized keyword is useful to synchronize an entire method.

119

120

121

122 Obtain the threads name
Method Meaning getName() Obtain the threads name setName() Set the name of a thread isAlive() Determine if the thread is still running join() Wait for thread to terminate sleep() Suspend a thread for specified time start() Start a thread by calling its run Method wait() This is used to make a thread wait notify() This is used to wake a thread that is waiting notifyall() This is used to wake all the threads that are waiting

123

124 LIFE CYCLE OF A THREAD During the life time of a thread, there are many states it can enter. They include: Newborn state Runnable state Running state Blocked state Dead state A thread is always in one of these five states. It can move from one state to another via a variety of ways as shown in Fig.

125

126 Newborn State When we create a thread object, the thread is born and is said to be in newborn state. The thread is not yet scheduled fur running. At this state we can do only one of the following things with it: Schedule it for running using start() method. Kill it using stop() method. If scheduled, it moves to the runnable state (Fig). If we attempt to use any other method at this stage, an exception will be thrown.

127

128 Runnable State The runnable state means that the thread is ready for execution and is waiting for the availability of the processor. That is, the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority, then they are given time slots for execution in round robin fashion, i.e. first-come, first-serve manner. The thread that relinquishes control joins the queue at the end and again waits for its turn. This process of assigning time to threads is known as time-slicing. However, if want a thread to relinquish control to another thread to equal priority before its turn comes, we can do so by using the yield() method. See Fig

129

130 Running State Running means that the processor has given its time to the thread for its execution. The thread runs until it relinquishes (release) control on its own or it is preempted by a higher priority thread. A running thread may relinquish its control in one of the following situations.

131 1. It has been suspended using suspend() method.
A suspended thread can be revived by using the resume() method. This approach is useful when we want to suspend a thread for some time due to certain reason, but do not want to kill it.

132 2. It has been made to sleep
2. It has been made to sleep. We can put a thread to sleep for a specified time period using the method sleep(time) where time is in milliseconds. This means that the thread is out of the queue during this time period. The thread re-enters the runnable state as soon as this time period is elapsed.

133 The thread can be scheduled to run again using the notify() method.
It has been told to wait until some event occurs. This is done using the wait() method. The thread can be scheduled to run again using the notify() method.

134 Blocked State A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping, or waiting in order to satisfy certain requirements. A blocked thread is considered "not runnable" but not dead and therefore fully qualified to run again.

135 Dead State Every thread has a life cycle. A running thread ends its life when it has completed executing its run() method. It is a natural death. However, we can kill if by sending the stop message to it at any state thus causing a premature death to it. A thread can be killed as soon it is born, or while it is running, or even when it is in "not runnable" (blocked) condition.

136 Thread Priorities When the threads are created and started, a 'thread scheduler' program in JVM will load them into memory and execute them. This scheduler will allot more JVM time to those threads which are having higher priorities. The priority numbers of a thread will change from 1 to 10. The minimum priority (shown by Thread.MIN_PRIORITY) of a thread is 1, and the maximum priority (Thread. MAX _ PRIORITY) is 10. The normal priority of a thread (Thread.NORM_PRIORITY) is 5.

137 In next Program, two threads are counting numbers from 1 to 10000.
When two tasks are assigned to two threads with different priorities, example, 2 and 5, then the thread with priority number 5 will be given more JVM time and hence it will complete the task earlier than the thread with priority number 2. See this effect in the following example to understand thread priorities. In next Program, two threads are counting numbers from 1 to The thread with priority number 5 is completing the counting first, even if it has been started later than the thread with priority number 2.

138

139 When condition i<10000 and I<10000000

140

141

142

143

144 INTER-THREAD COMMUNICATION
Inter-thread communication can be defined as the exchange of messages between two or more threads. The transfer of messages takes place before or after the change of state of a thread. For example, an active thread may notify to another suspended thread just before switching to the suspend state. Java implements inter-thread communication with the help of following three methods:

145 notifyall():Resumes all the threads that are in sleep mode.
notify(): Resumes the first thread that went into the sleep mode. The object class declaration of notify() method is shown below: final void notify() notifyall():Resumes all the threads that are in sleep mode. The execution of these threads happens as per priority. The object class declaration of notifyall() method is shown below: final void notifyall()

146 One can also specify the time for which the thread has to wait.
wait(): Sends the calling thread into the sleep mode. This thread can now be activated only by notify() or notifyall() methods. One can also specify the time for which the thread has to wait. The desired waiting time period is specified as an argument to the wait() method. The object class declaration of wait() method is shown below: final void wait()

147 Suspending, Resuming and Stopping Thread
The functions of suspending, resuming and stopping a thread are performed using Boolean type flags in a multithreading program. These flags store the current status of the thread. The run() method works according to the current boolean value of these three flags. For instance, if the suspend flag is set to true then run() will suspend the execution of the currently running thread. Similarly, if the resume flag is set to true then run() will resume the execution of the suspended thread. Apart from suspend and resume, a thread will get terminated once the stop flag of the thread is set to true.

148

149

150

151 What is Thread deadlock?
When a thread has locked an object and waiting for another object to be released by another thread, and the other thread is also waiting for the first thread to release the first object, both the threads will continue waiting forever. This is called Thread deadlock'.


Download ppt "THREADS."

Similar presentations


Ads by Google