Synchronization



Synchronization is a process of blocking the object so that only one thread can access that object. When object is locked by a thread then other threads has to wait until the lock is released.
We can implement the synchronization in two ways: ---------

  •    Method level synchronization
  •    Block level synchronization
Method level synchronization:------ syntax
                         Synchronized return-type method_name()
                                   {
                                            --------------
                                            -------------
                                      }
Example :-------
                                 Class Hello
                                {
                                  Synchronized  void m1()
                                          {
                                             ------------
                                                           }
                                         void m2()
                                            {
                                                  --------
                                                            }
                                     }
Assume that there are two threads t1 and t2 which are trying to use method m1 with same Hello class object.

When t1 gets the CPU time then object h will be locked by t1 then t1 execute the m1 method completely .as long as t1 is executing m1 t2 has to wait to use that object.

Assume that there are two threads t1 and t2 . t1 is  trying to invoke m1 with h1 and t2 is trying to invoke m1 with h2.In this case both the thread can execute the m1 concurrently.

In the case of method level Synchronization the object which you are using to invoke the synchronized method will be locked.
Class Hello
{
Synchronized void m1()
{
System.out.println(“1”);                           
 System.out.println(“2”);
 System.out.println(“3”);
System.out.println(“4”);
 }
}
Block level synchronization :------- syntax
                              Synchronized (Object)
                                   {
                                            --------------
                                            -------------
                                      }
Example :-------
                                 Class Hello
                                {
                                   void m1()
                                          {
                                          System.out.println(“1”);
                      Synchronized(object)
                          {
                                       System.out.println(“2”);
                                      System.out.println(“3”);
                                                                                  }
                                      System.out.println(“4”);
                                                 }
                                     }

Scenario :---------
                                          Void m1(Array_list al)
                                           { ----------------------
                                          Synchronized(al)
                                              { -------------------  } }
When you using the synchronization block then the object which is you are passing as a parameter to the synchronization block will be locked. 

Some important scenarios with synchronization----

Case I :-----There is a class Hello with two methods m1 and m2 and both m1 and m2 are synchronized .one object called h1 is created for the Hello class. there are two threads t1 and t2 .t1 is trying to access m1 with h. what will happen when t2 is trying to access m2 with h.?
                          t2 is unable to access h because h is already locked  by t1 
Case II:------- There is a class Hello with two methods m1 and m2 and  m1 is not synchronized  and m2 is  synchronized .one object called h is created for the Hello class. there are two threads t1 and t2 .t2 is trying to access m2 with object h. what will happen when t1 is trying to access m1 with the same h.?
                                  t1 can access m1.
                
                              Hello h=new Hello()                                                                 
                             visible only for Synchronized methods

Case III :------- There is a class Hello with two methods m1 and m2 .m1 is static  and  synchronized  and m2 is  non-static and synchronized .one object called h is created for the Hello class. there are two threads t1 and t2 .t1 is trying to access m1 with class name Hello . what will happen when t2 is trying to access m2 with the same object  h?
Default object of type java.lang will be locked h will be locked
Case IV:--------- There is a class Hello with two methods m1 and m2 and both m1 and m2 are synchronized .two object is created called h1 and h2 . there are two threads running called  t1 and t2 .t1 is trying to access m1 with object  h1. what will happen when t2 is trying to access m2 with h2?
                                           t2 can access m2 with h2  without any problem.
Case V---- there is a class Hello with two method m1 and m2 where m1 and m2 are not synchronized there  are two threads called A and B when A is accessing m1  at the same time B is trying to access m2 then what will happen ? ------- B can access m2 concurrently while A is accessing m1().because both m1 and m2 are not synchronized .
Case VI:------- There is a class Hello with two methods m1 and m2 .m1 is non static  and  synchronized  and m2 is  non-static and synchronized .one object called h is created for the Hello class. there are two threads t1 and t2 .what happen when thread t1 and t2 are  trying to access m1 and m2 respectively at a time ?
                 A and B can access m1() and m2() concurrently .
Case VII :------ class Hello has two static synchronization method m1 and m2.when thread A is accessing m1() thread B is trying to access m2() what will be  happened ?--------in this case B is unable to access m2().

NOTE :----  at any time only one thread can access any method whether it is synchronized or non-synchronized .
v  In  non-synchronized  method  at a time multiple threads are accessing that same method whenever they get time to execute this method. .
v  By default each object has default lock but lock is enabled by synchronization keyword .

 

0 comments :

Post a Comment