Saturday, 6 May 2017

14. Synchronization - object level lock


1. What is Synchronization in Thread – Object level lock
1.       Internally synchronization concept is implemented by using lock.
2.       Every object in java has a unique lock.
3.       Whenever we are using synchronized keyword then only lock concept will come into the picture.
4.       If a thread wants to execute synchronized method on the given object, first it has to get lock of that object.
5.       Once thread got the lock then it is allowed to execute any synchronized method on that object.
6.       Once method execution completes automatically thread releases lock.
7.       Acquiring and releasing the lock internally takes care by  JVM and programmer not responsible for this activity.
8.       While a thread executing synchronized method on given object, the remaining threads not allowed to execute simultaneously on same object, but remaining threads allowed to execute non synchronized methods simultaneously.
9.       In below example
a.       If t1 executes m1(), then it locks ‘x’ object until completion of m1();
b.      If t2 came to execute m1() in the middle of t1 execution, then t2 will require lock of ‘x’, but lock is not available, presently t1 has the lock, so t2 will be wait for lock of ‘x’ object(t2 will get lock after completion of m1() by t1 thread).
c.       If t3 came to execute m2() in middle of m1() execution by t1 thread , so t3 also waits because lock is not available.
d.      If t4 came to execute m3(), in the middle of m1() execution by t1, but m3() is non synchronized method so lock is not required for execution of m3(), so t4 will executes m3().
public class X {
  synchronized void m1(){......}
  synchronized void m2(){......}
  void m3(){.......}
}
10.   Lock concept is implemented based on object but not based on method.
11.   In practically we will write update/insert/delete operations in synchronized method.
12.   Read operations in non synchronized method.
public class Dispaly {
  public synchronized void wish(String name)Throws IE{
        for(int i=0; i<10; i++){
              System.out.println(“Good morning ”+name);
              Thread.sleep(2000);
        }
  }
}
Class MyThread extends Thread{
  Display d;
  String name;
  MyThread(Display d, String name){
        this.d = d;
        this.name = name;
  }
  Public void run(){
        d.wish();
  }
}
Class MainDemo{
  public static void main(String[] args)throws Exception{
        Dispaly d = new Display();
        MyThread thread1 = new MyThread(d,”Rama”);
        MyThread thread2 = new MyThread(d,”Seetha”);
        thread1.start();
        thread2.start();
  }
}
o/p:
13.   After completion one thread only next thread allows to execute wish().
14.   If we are not declaring wish() as synchronized then both threads will be executed simultaneously and hence we will get irregular output.
Wish() is synchronized* (Regular output)
Wish() is not synchronized (irregular output)
Good morning Rama
Good morning Rama
Good morning Rama
Good morning Seetha
Good morning Rama
Good morning Rama
Good morning Seetha
Good morning Seetha
Good morning Seetha
Good morning Rama
Good morning Seetha
Good morning Seetha
*in may be order will change if rama thread will execute first or seethe thread will execute first.
15.   If two threads will execute wish() with two different objects, then this case synchronization is not required.
16.   Because t1 will execute d1.wish() , so it has lock of d1 object., and t2 will execute d2.wish(), so t2 has a lock of d2 object.
17.   So there is no conflict on threads on their object. So we will get irregular output because both threads will execute on same time.
18.   If multiple threads are operating on same java object then synchronization is required.
19.   If multiple threads are operating on multiple java objects then synchronization is not required.
20.   If in above example changing main() like below
Class MainDemo{
  public static void main(String[] args)throws Exception{
        Dispaly d1 = new Display();
        Dispaly d2 = new Display();
        MyThread thread1 = new MyThread(d1,”Rama”);
        MyThread thread2 = new MyThread(d2,”Seetha”);
        thread1.start();
        thread2.start();
        System.out.println(“end of main thread”);
  }
}
Wish() is synchronized*
Good morning Rama
Good morning Seetha
Good morning Rama
Good morning Seetha
Good morning Rama
Good morning Seetha



No comments:

Post a Comment

3. Java Program to create Binary Tree