Presentation is loading. Please wait.

Presentation is loading. Please wait.

6/11/2016 DEPT OF CSE 1 JAVA. 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING.

Similar presentations


Presentation on theme: "6/11/2016 DEPT OF CSE 1 JAVA. 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING."— Presentation transcript:

1 6/11/2016 DEPT OF CSE 1 JAVA

2 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING

3 6/11/2016 DEPT OF CSE 3

4 6/11/2016 DEPT OF CSE 4 In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code Multitasking threads require less overhead than multitasking processes Processes are heavyweight tasks that require their own separate address spaces Interprocess communication is expensive and limited Context switching from one process to another is also costly

5 6/11/2016 DEPT OF CSE 5 Threads, on the other hand, are lightweight They share the same address space and cooperatively share the same heavyweight process Interthread communication is inexpensive, and context switching from one thread to the next is low cost Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum

6 6/11/2016 DEPT OF CSE 6 The Java Thread Model

7 6/11/2016 DEPT OF CSE 7 Single-threaded systems use an approach called an event loop with polling In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next Once this polling mechanism returns with, say, a signal that a network file is ready to be read, then the event loop dispatches control to the appropriate event handler Until this event handler returns, nothing else can happen in the system

8 6/11/2016 DEPT OF CSE 8 This wastes CPU time In general, in a singled-threaded environment, when a thread blocks (that is, suspends execution) because it is waiting for some resource, the entire program stops running When a thread blocks in a Java program, only the single thread that is blocked pauses; All other threads continue to run.

9 6/11/2016 DEPT OF CSE 9 Threads exist in several states A thread can be running It can be ready to run as soon as it gets CPU time A running thread can be suspended, which temporarily suspends its activity A suspended thread can then be resumed, allowing it to pick up where it left off A thread can be blocked when waiting for a resource At any time, a thread can be terminated, which halts its execution immediately. Once terminated, a thread cannot be resumed

10 6/11/2016 DEPT OF CSE 10

11 6/11/2016 DEPT OF CSE 11

12 6/11/2016 DEPT OF CSE 12 Thread Priorities Java assigns to each thread a priority that determines how that thread should be treated with respect to the others Thread priorities are integers that specify the relative priority of one thread to another A thread’s priority is used to decide when to switch from one running thread to the next This is called a context switch

13 6/11/2016 DEPT OF CSE 13 The rules that determine when a context switch takes place are: A thread can voluntarily relinquish control A thread can be preempted by a higher- priority thread

14 6/11/2016 DEPT OF CSE 14 Synchronization Java implements synchronization using monitor Once a thread enters a monitor, all other threads must wait until that thread exits the monitor Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object

15 6/11/2016 DEPT OF CSE 15 Messaging Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have Java’s messaging system allows a thread to enter a synchronized method on an object, and then wait there until some other thread explicitly notifies it to come out

16 6/11/2016 DEPT OF CSE 16 The Thread Class and the Runnable Interface Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable To create a new thread, our program will either extend Thread or implement the Runnable interface

17 6/11/2016 DEPT OF CSE 17 The Thread class defines several methods that help manage threads

18 6/11/2016 DEPT OF CSE 18 The Main Thread When a Java program starts up, one thread begins running immediately This is usually called the main thread of your program, because it is the one that is executed when your program begins It is the thread from which other “child” threads will be spawned Often it must be the last thread to finish execution because it performs various shutdown actions

19 6/11/2016 DEPT OF CSE 19 Although the main thread is created automatically when your program is started, it can be controlled through a Thread object To do so, we must obtain a reference to it by calling the method currentThread( ), which is a public static member of Thread static Thread currentThread( ) This method returns a reference to the thread in which it is called

20 6/11/2016 DEPT OF CSE 20 // Controlling the main Thread. class CurrentThreadDemo { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println("Current thread: " + t); // change the name of the thread t.setName("My Thread"); System.out.println("After name change: " + t); try { for(int n = 5; n > 0; n--) { System.out.println(n); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted"); } } }

21 6/11/2016 DEPT OF CSE 21 The argument to sleep( ) specifies the delay period in milliseconds Output: Current thread: Thread[main,5,main] After name change: Thread[My Thread,5,main] 5 4 3 2 1 t displays the name of the thread, its priority, and the name of its group

22 6/11/2016 DEPT OF CSE 22 By default, the name of the main thread is main. Its priority is 5, which is the default value, and main is also the name of the group of threads to which this thread belongs A thread group is a data structure that controls the state of a collection of threads as a whole

23 6/11/2016 DEPT OF CSE 23 We can set the name of a thread by using setName( ) We can obtain the name of a thread by calling getName( ) static void sleep(long milliseconds) throws InterruptedException static void sleep(long milliseconds, int nanoseconds) throws InterruptedException

24 6/11/2016 DEPT OF CSE 24 Creating a Thread

25 6/11/2016 DEPT OF CSE 25 We create a thread by instantiating an object of type Thread We can implement the Runnable interface We can extend the Thread class, itself

26 6/11/2016 DEPT OF CSE 26 Implementing Runnable We can construct a thread on any object that implements Runnable To implement Runnable, a class need only implement a single method called run( ), which is declared like this: public void run( )

27 6/11/2016 DEPT OF CSE 27 Inside run( ), we will define the code that constitutes the new thread run( ) establishes the entry point for another, concurrent thread of execution within our program After we create a class that implements Runnable, we will instantiate an object of type Thread from within that class

28 6/11/2016 DEPT OF CSE 28 The constructor is: Thread(Runnable threadOb, String threadName) threadOb is an instance of a class that implements the Runnable interface; This defines where execution of the thread will begin The name of the new thread is specified by threadName After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread

29 6/11/2016 DEPT OF CSE 29 In essence, start( ) executes a call to run( ) void start( )

30 6/11/2016 DEPT OF CSE 30

31 6/11/2016 DEPT OF CSE 31 // Create a second thread. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); }

32 6/11/2016 DEPT OF CSE 32 class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }

33 6/11/2016 DEPT OF CSE 33 Output: Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.

34 6/11/2016 DEPT OF CSE 34 Extending Thread The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class The extending class must override the run( ) method, which is the entry point for the new thread It must also call start( ) to begin execution of the new thread

35 6/11/2016 DEPT OF CSE 35 // Create a second thread by extending Thread class NewThread extends Thread { NewThread() { // Create a new, second thread super("Demo Thread"); System.out.println("Child thread: " + this); start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); }

36 6/11/2016 DEPT OF CSE 36 class ExtendThread { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }

37 6/11/2016 DEPT OF CSE 37 Call to super( ) inside NewThread invokes the following form of the Thread constructor: public Thread(String threadName) Here, threadName specifies the name of the thread

38 6/11/2016 DEPT OF CSE 38 Choosing an Approach Which approach is better? The Thread class defines several methods that can be overridden by a derived class Of these methods, the only one that must be overridden is run( ) This is, of course, the same method required when you implement Runnable Many Java programmers feel that classes should be extended only when they are being enhanced or modified in some way

39 6/11/2016 DEPT OF CSE 39 So, if you will not be overriding any of Thread’s other methods, it is probably best simply to implement Runnable But it is left to the programmers;

40 6/11/2016 DEPT OF CSE 40 Creating Multiple Threads

41 6/11/2016 DEPT OF CSE 41 So far, we have been using only two threads: the main thread and one child thread However, our program can spawn as many threads as it needs

42 6/11/2016 DEPT OF CSE 42 // Create multiple threads. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + "Interrupted"); } System.out.println(name + " exiting."); }

43 6/11/2016 DEPT OF CSE 43 class MultiThreadDemo { public static void main(String args[]) { new NewThread("One"); // start threads new NewThread("Two"); new NewThread("Three"); try { // wait for other threads to end Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); }

44 6/11/2016 DEPT OF CSE 44 The output from this program is shown here: New thread: Thread[One,5,main] New thread: Thread[Two,5,main] New thread: Thread[Three,5,main] One: 5 Two: 5 Three: 5 One: 4 Two: 4 Three: 4 One: 3 Three: 3 Two: 3 One: 2 Three: 2 Two: 2 One: 1 Three: 1 Two: 1 One exiting. Two exiting. Three exiting. Main thread exiting.

45 6/11/2016 DEPT OF CSE 45 Using isAlive( ) and join( ) How can one thread know when another thread has ended? Two ways exist to determine whether a thread has finished First, you can call isAlive( ) on the thread This method is defined by Thread, and its general form is final boolean isAlive( )

46 6/11/2016 DEPT OF CSE 46 The isAlive( ) method returns true if the thread upon which it is called is still running It returns false otherwise The method that you will more commonly use to wait for a thread to finish is called join( ), shown here: final void join( ) throws InterruptedException This method waits until the thread on which it is called terminates

47 6/11/2016 DEPT OF CSE 47 // Using join() to wait for threads to finish. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); }

48 6/11/2016 DEPT OF CSE 48 class DemoJoin { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); NewThread ob3 = new NewThread("Three"); System.out.println("Thread One is alive: " + ob1.t.isAlive()); System.out.println("Thread Two is alive: " + ob2.t.isAlive()); System.out.println("Thread Three is alive: " + ob3.t.isAlive()); // wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Thread One is alive: " + ob1.t.isAlive()); System.out.println("Thread Two is alive: " + ob2.t.isAlive()); System.out.println("Thread Three is alive: " + ob3.t.isAlive()); System.out.println("Main thread exiting."); }

49 6/11/2016 DEPT OF CSE 49 Sample output from this program is shown here: New thread: Thread[One,5,main] New thread: Thread[Two,5,main] New thread: Thread[Three,5,main] Thread One is alive: true Thread Two is alive: true Thread Three is alive: true Waiting for threads to finish. One: 5 Two: 5 Three: 5 One: 4 Two: 4 Three: 4 One: 3 Two: 3 Three: 3 One: 2 Two: 2 Three: 2 One: 1 Two: 1 Three: 1 Two exiting. Three exiting. One exiting. Thread One is alive: false Thread Two is alive: false Thread Three is alive: false Main thread exiting.

50 Thread Priorities final void setPriority(int level) The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY with values are 1 and 10, respectively. Default priority, NORM_PRIORITY, is currently 5. final int getPriority( ) 6/11/2016 DEPT OF CSE 50

51 6/11/201651 volatile The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program In a multithreaded program, sometimes, two or more threads share the same instance variable. For efficiency considerations, each thread can keep its own, private copy of such a shared variable. The real (or master) copy of the variable is updated at various times, such as when a synchronized method is entered. While this approach works fine, it may be inefficient at times. In some cases, all that really matters is that the master copy of a variable always reflects its current state. To ensure this, simply specify the variable as volatile, which tells the compiler that it must always use the master copy of a volatile variable (or, at least, always keep any private copies up to date with the master copy, and vice versa).

52 6/11/201652 Volatile The Java language specification allows the runtime system to keep a local copy of a variable in each thread that refers to it. These thread-local copies of a variable act like a cache to improve the performance of multi-threaded programs, they avoid having to check the true master value of the variable every time it is needed. One consequence of this thread cache behavior is that at the moment of execution the value of thread-local variables may be different from the true master value of the variable. For variables whose value does not change frequently and would not critically affect the behavior of a program, thread-local cached values are a tolerable compromise of precision for the sake of better performance, if not they should be controlled.

53 // Demonstrate thread priorities. class clicker implements Runnable { int click = 0; Thread t; // volatile is used here to ensure that the value of running is examined each time the following loop iterates. The use of volatile tells Java that running may change in ways not directly apparent in the immediate code. private volatile boolean running = true; public clicker(int p) { t = new Thread(this); t.setPriority(p); } public void run() { while (running) { click++; } } public void stop() { running = false; } public void start() { t.start(); } } 6/11/2016 DEPT OF CSE 53

54 class HiLoPri { public static void main(String args[]) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); clicker hi = new clicker(Thread.NORM_PRIORITY + 2); clicker lo = new clicker(Thread.NORM_PRIORITY - 2); lo.start(); hi.start(); try { Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } lo.stop(); hi.stop(); // Wait for child threads to terminate. try { hi.t.join(); lo.t.join(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Low-priority thread: " + lo.click); System.out.println("High-priority thread: " + hi.click); } 6/11/2016 DEPT OF CSE 54

55 The higher-priority thread got approximately 90 percent of the CPU time. Low-priority thread: 4408112 High-priority thread: 589626904 6/11/2016 DEPT OF CSE 55

56 6/11/2016 DEPT OF CSE 56 Synchronization When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time The process by which this is achieved is called synchronization Key to synchronization is the concept of the monitor (like semaphore)

57 6/11/2016 DEPT OF CSE 57 A monitor is an object that is used as a mutually exclusive lock, or mutex Only one thread can own a monitor at a given time When a thread acquires a lock, it is said to have entered the monitor All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor These other threads are said to be waiting for the monitor

58 6/11/2016 DEPT OF CSE 58 Using Synchronized Methods // This program is not synchronized. class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); }

59 6/11/2016 DEPT OF CSE 59 class Caller implements Runnable { String msg; Callme target; Thread t; public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); } public void run() { target.call(msg); }

60 6/11/2016 DEPT OF CSE 60 class Synch { public static void main(String args[]) { Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); // wait for threads to end try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); }

61 6/11/2016 DEPT OF CSE 61 Here is the output produced by this program: [Hello[Synchronized[World] ]

62 6/11/2016 DEPT OF CSE 62 In this program, nothing exists to stop all three threads from calling the same method, on the same object, at the same time This is known as a race condition, because the three threads are racing each other to complete the method In most situations, a race condition is more subtle and less predictable, because you can’t be sure when the context switch will occur

63 6/11/2016 DEPT OF CSE 63 To fix the preceding program, you must serialize access to call( ) To do this, you simply need to precede call()’s definition with the keyword synchronized class Callme { synchronized void call(String msg) {... This prevents other threads from entering call( ) while another thread is using it

64 6/11/2016 DEPT OF CSE 64 After synchronized has been added to call(), the output of the program is as follows: [Hello] [Synchronized] [World] Once a thread enters any synchronized method on an instance, no other thread can enter any other synchronized method on the same instance However, nonsynchronized methods on that instance will continue to be callable

65 6/11/2016 DEPT OF CSE 65 The synchronized Statement Imagine that you want to synchronize access to objects of a class that was not designed for multithreaded access That is, the class does not use synchronized methods Further, this class was not created by you, but by a third party, and you do not have access to the source code Thus, you can’t add synchronized to the appropriate methods within the class

66 6/11/2016 DEPT OF CSE 66 How can access to an object of this class be synchronized?

67 6/11/2016 DEPT OF CSE 67 You simply put calls to the methods defined by this class inside a synchronized block

68 6/11/2016 DEPT OF CSE 68 This is the general form of the synchronized statement: synchronized(object) { // statements to be synchronized } Here, object is a reference to the object being synchronized A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object’s monitor

69 6/11/2016 DEPT OF CSE 69 // This program uses a synchronized block. class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); }

70 6/11/2016 DEPT OF CSE 70 class Caller implements Runnable { String msg; Callme target; Thread t; public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); } // synchronize calls to call() public void run() { synchronized(target) { // synchronized block target.call(msg); }

71 6/11/2016 DEPT OF CSE 71 class Synch1 { public static void main(String args[]) { Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); // wait for threads to end try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); }

72 6/11/2016 DEPT OF CSE 72 Interthread Communication

73 6/11/2016 DEPT OF CSE 73 Java includes an elegant interprocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods All three methods can be called only from within a synchronized context wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( )

74 6/11/2016 DEPT OF CSE 74 notify( ) wakes up the first thread that called wait( ) on the same object notifyAll( ) wakes up all the threads that called wait( ) on the same object; The highest priority thread will run first.

75 6/11/2016 DEPT OF CSE 75

76 Producer ConsumerProblem // An incorrect implementation of a producer and consumer. class Q { int n; synchronized int get() { System.out.println("Got: " + n); return n; } synchronized void put(int n) { this.n = n; System.out.println("Put: " + n); }

77 Producer ConsumerProblem class Producer implements Runnable { Q q; Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(true) { q.put(i++); }

78 Producer ConsumerProblem class Consumer implements Runnable { Q q; Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { while(true) { q.get(); } }

79 Producer ConsumerProblem class PC { public static void main(String args[]) { Q q = new Q(); new Producer(q); new Consumer(q); System.out.println("Press Control-C to stop."); }

80 6/11/2016 DEPT OF CSE 80 class Q { int n; boolean valueSet = false; synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Got: " + n); valueSet = false; notify(); return n; }

81 6/11/2016 DEPT OF CSE 81 synchronized void put(int n) { if(valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } this.n = n; valueSet = true; System.out.println("Put: " + n); notify(); }

82 6/11/2016 DEPT OF CSE 82 class Producer implements Runnable { Q q; Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(true) { q.put(i++); }

83 6/11/2016 DEPT OF CSE 83 class Consumer implements Runnable { Q q; Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { while(true) { q.get(); }

84 6/11/2016 DEPT OF CSE 84 class ProducerConsumer { public static void main(String args[]) { Q q = new Q(); new Producer(q); new Consumer(q); System.out.println("Press Control-C to stop."); }

85 6/11/2016 DEPT OF CSE 85 Output: Put: 1 Got: 1 Put: 2 Got: 2 Put: 3 Got: 3 Put: 4 Got: 4 Put: 5 Got: 5

86 Deadlock Deadlock occurs when two threads have a circular dependency on a pair of synchronized objects. Suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will block as expected. If the thread in Y, in turn, tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete. 6/11/2016 DEPT OF CSE 86

87 Deadlock is a difficult error to debug for two reasons: ■ Two threads time-slice in just the right way. ■ It may involve more than two threads and two synchronized objects. 6/11/2016 DEPT OF CSE 87

88 // An example of deadlock. class A { synchronized void foo(B b) { String name = Thread.currentThread().getName(); System.out.println(name + " entered A.foo"); try { Thread.sleep(1000); } catch(Exception e) { System.out.println("A Interrupted"); } System.out.println(name + " trying to call B.last()"); b.last(); } synchronized void last() { System.out.println("Inside A.last"); } 6/11/2016 DEPT OF CSE 88

89 class B { synchronized void bar(A a) { String name = Thread.currentThread().getName(); System.out.println(name + " entered B.bar"); try { Thread.sleep(1000); } catch(Exception e) { System.out.println("B Interrupted"); } System.out.println(name + " trying to call A.last()"); a.last(); } synchronized void last() { System.out.println("Inside A.last"); } 6/11/2016 DEPT OF CSE 89

90 class Deadlock implements Runnable { A a = new A(); B b = new B(); Deadlock() { Thread.currentThread().setName("MainThread"); Thread t = new Thread(this, "RacingThread"); t.start(); a.foo(b); // get lock on a in this thread. System.out.println("Back in main thread"); } public void run() { b.bar(a); // get lock on b in other thread. System.out.println("Back in other thread"); } public static void main(String args[]) { new Deadlock(); } 6/11/2016 DEPT OF CSE 90

91 The output is as shown : MainThread entered A.foo RacingThread entered B.bar MainThread trying to call B.last() RacingThread trying to call A.last() 6/11/2016 DEPT OF CSE 91

92 6/11/2016 DEPT OF CSE 92

93 6/11/2016 DEPT OF CSE 93

94 Wait and Notify: Code Consumer: synchronized (lock) { while (!resourceAvailable()) { lock.wait(); } consumeResource(); }

95 Wait and Notify: Code Producer: produceResource(); synchronized (lock) { lock.notifyAll(); }

96 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

97 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

98 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

99 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

100 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

101 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

102 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

103 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

104 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

105 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

106 Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }

107 Wait/Notify: Details Often the lock object is the resource itself Sometimes the lock object is the producer thread itself

108 Wait/Notify: Details Must loop on wait(), in case another thread grabs the resource...  After you are notified  Before you acquire the lock and return from wait() Use lock.notifyAll() if there may be more than one waiting thread

109 Class Nthread implements Runnable { Thread t; int r=0,n; rthread(int a) {t=newthread(); T.start(); R=a; } Public void run() { for( int i=0;i<rowsum.mat[0].length;i++) {rs=rowsum.mat[r][i]; try { Thread.sleep(1000); }

110 Catch(InterruptedException e) { system.out.println(“Interruped “);} } } } Class Rowsum { static int mat[][]={{1,2,3},{4,5,6},{7,8,9}}; public static void main(String s[]) { rthread rt[] =new rthread[10]; for(int i=0;i<mat.length;i++) { rt[i]=new rthread[i]; } try { Thread.sleep(500);} Catch(Exception e){ } Try { for(int i=0;i<mat.length;i++)

111 rt[i].t.join(); } catch(InterruptedException e){ } int sum=0; for(int i=0;i<mat.length;i++) sum t=rt[i].rs; System.out.println(“ SUM is : “+sum); }

112 6/11/2016112 DeadLock This occurs when two threads have a circular dependency on a pair of synchronized objects. X Y T1 T2

113 6/11/2016113 EXample // An example of deadlock. class A { synchronized void foo(B b) { String name = Thread.currentThread().getName(); System.out.println(name + " entered A.foo"); try { Thread.sleep(1000); } catch(Exception e) { System.out.println("A Interrupted"); } System.out.println(name + " trying to call B.last()"); b.last();

114 6/11/2016114 } synchronized void last() { System.out.println("Inside A.last"); } class B { synchronized void bar(A a) { String name = Thread.currentThread().getName(); System.out.println(name + " entered B.bar"); try { Thread.sleep(1000); } catch(Exception e) { System.out.println("B Interrupted");

115 6/11/2016115 } System.out.println(name + " trying to call A.last()"); a.last(); } synchronized void last() { System.out.println("Inside A.last"); } class Deadlock implements Runnable { A a = new A(); B b = new B(); Deadlock() { Thread.currentThread().setName("MainThread");

116 6/11/2016116 Thread t = new Thread(this, "RacingThread"); t.start(); a.foo(b); // get lock on a in this thread. System.out.println("Back in main thread"); } public void run() { b.bar(a); // get lock on b in other thread. System.out.println("Back in other thread"); } public static void main(String args[]) { new Deadlock(); }

117 6/11/2016 DEPT OF CSE 117 END OF UNIT - 3


Download ppt "6/11/2016 DEPT OF CSE 1 JAVA. 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING."

Similar presentations


Ads by Google