22 multi threading iv


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

22 multi threading iv

  1. 1. 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. • Java provides unique, language-level support for it. Key to synchronization is the concept of the monitor (also called a semaphore). • 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. A thread that owns a monitor can reenter the same monitor if it so desires.
  2. 2. • We can synchronize our code in either of two ways. Both involve the use of the synchronized keyword, and both are examined here. • Using synchronized Methods • The synchronized Statement • synchronized Methods : All objects have their own implicit monitor associated with them. To enter an object’s monitor, just call a method that has been modified with the synchronized keyword. While a thread is inside a synchronized method, all other threads that try to call it (or any other synchronized method) on the same instance have to wait. To exit the monitor and relinquish control of the object to the next waiting thread, the owner of the monitor simply returns from the synchronized method.
  3. 3. class callme{ static int i=1; //synchronized static void print() { System.out.print("( HI "); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println(i+" )"); i++; } } class A extends Thread { public void run() { for(int i=1;i<=5;i++) callme.print(); } }
  4. 4. class B extends Thread{ public void run() { for(int i=1;i<=5;i++) callme.print(); } } class test { public static void main(String args[]) { A th1= new A(); th1.start(); // new A().start(); B th2=new B(); th2.start(); } }
  5. 5. ( HI ( HI 1 ) 1 ) ( HI ( HI 3 ) ( HI 3 ) ( HI 5 ) ( HI 5 ) ( HI 7 ) ( HI 7 ) ( HI 9 ) 9 ) • Here the value of variable i is updated in unordered way by both the threads A and B. The output may change for each run. This can lead to a series problem if i is some important shared data or a control flag. The output printing is also not in order. To solve the problem we need synchronization over the print() method. • If the print() method is defined as: synchronized static void print(){---------}
  6. 6. • Then the method is synchronized and the output will be-> ( HI 1 ) ( HI 2 ) ( HI 3 ) ( HI 4 ) ( HI 5 ) ( HI 6 ) ( HI 7 ) ( HI 8 ) ( HI 9 ) ( HI 10 ) • Also the output will remain same for each run.(As only one thread can execute the print() method at a time and other thread has to wait for completion of current call to print method.)
  7. 7. • The synchronized Statement: While creating synchronized methods within classes that you create is an easy and effective means of achieving synchronization, it will not work in all cases. • Consider the following. Imagine that we 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 us, but by a third party, and we do not have access to the source code. Thus, we can’t add synchronized to the appropriate methods within the class. How can access to an object of this class be synchronized? • The solution to this problem is quite easy: simply put calls to the methods defined by this class inside a synchronized block. 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.
  8. 8. class callme{ int i=1; //non-static member void print() { //non-static member System.out.print("( HI "); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println(i+" )"); i++; } } class A extends Thread { callme obj; A(callme target) { obj=target; } public void run() { for(int i=1;i<=5;i++) { synchronized(obj) { obj.print(); } } } }
  9. 9. class B extends Thread{ callme obj; B(callme target){ obj=target; } public void run() { for(int i=1;i<=5;i++) { synchronized(obj) {obj.print(); } } } } class test { public static void main(String args[]) { callme obj1=new callme(); A th1= new A(obj1); th1.start(); // new A().start(); B th2=new B(obj1); th2.start(); } }
  10. 10. • Here both thread are referring to the same instance/object of class callme. The call to method print() is in synchronized block this time. ( HI 1 ) ( HI 2 ) ( HI 3 ) ( HI 4 ) …….. ( HI 9 ) ( HI 10 ) So here without changing the original class callme the call to print method is executed in synchronized block. This will have the same effect as the previous case.(Where the print method is ynchronized.)
  11. 11. Interthread Communication • Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, appropriate action is taken. This wastes CPU time. • To avoid polling, Java includes an elegant interprocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. 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( ). • 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.
  12. 12. • These methods are declared within Object, as shown here: final void wait( ) throws InterruptedException final void notify( ) final void notifyAll( )
  13. 13. class A { public static void main(String[]args)throws InterruptedException { B b =new B(); b.start(); synchronized(b) //thread got lock { System.out.println("Calling wait method"); b.wait(); System.out.println("Got notification"); } System.out.println(b.total); } } class B extends Thread{ int total=0; public void run() { synchronized (this) //.thread got lock { System.out.println("Starting calculation"); for(int i=0;i<=1000;i++) { total=total+I; } System.out.println("Giving notification call"); notify(); //thread releases lock again }} }
  14. 14. Calling wait method Starting calculation Giving notification call Got notification 500500