Saturday 1 March 2014

Synchronized Methods

Java programming language provides two strategies for basic synchronization :

Those are
   1. synchronized methods
   2. synchronized statements Block.

How to make a method synchronized
To make a method synchronized, simply add the synchronized keyword to its declaration

Example
synchronized int add(int a, int b)

Some points to remember about synchronized methods
1. It is not possible for two invocations of synchronized methods on the same object to interleave, i.e, if one thread executing a synchronized method of object obj1, then no other thread is able to execute any synchronized method of object obj1.

Example
class SynchMethEx implements Runnable{
 public void run(){
  printA();
 }

 synchronized void printA(){
  for(int i=0; i < 5; i++){
   System.out.println(Thread.currentThread().getName() + " A");
   try{
    Thread.currentThread().sleep(10);
   }
   catch(InterruptedException e){
   }
  }
 }

 synchronized void printB(){
  for(int i=0; i < 5; i++){
   System.out.println(Thread.currentThread().getName() + " B");
   try{
    Thread.currentThread().sleep(10);
   }
   catch(InterruptedException e){
   }
  }
 }

 public static void main(String args[]){
  SynchMethEx task1 = new SynchMethEx();
  Thread t1 = new Thread(task1);
  t1.setName("thread1");
  t1.start();
  task1.printB();
 }
}

Sample Output    
main B
main B
main B
main B
main B
thread1 A
thread1 A
thread1 A
thread1 A
thread1 A

Explanation
We have two synchronized methods printA and printB. Bothe have a printing statement inside. When one thread, let us assume main thread calls the printB method “task1.printB()” then thread t1 will wait until main thread finishes its execution with printB().

2. Okey, fine I understood that, if one thread executing a synchronized method of an object “task1” no other thread able to execute any synchronized method of object “task1”. Then what about non synchronized methods.
Non synchronized methods don't have any restriction on execution. I am removing the synchronization for the method printB in the above program, like below. Now compare the output.

class SynchMethEx implements Runnable{
 public void run(){
  printA();
 }

 synchronized void printA(){
  for(int i=0; i < 5; i++){
   System.out.println(Thread.currentThread().getName() + " A");
   try{
    Thread.currentThread().sleep(10);
   }
   catch(InterruptedException e){
   }
  }
 }

 void printB(){
  for(int i=0; i < 5; i++){
   System.out.println(Thread.currentThread().getName() + " B");
   try{
    Thread.currentThread().sleep(10);
   }
   catch(InterruptedException e){
   }
  }
 }

 public static void main(String args[]){
  SynchMethEx task1 = new SynchMethEx();
  Thread t1 = new Thread(task1);
  t1.setName("thread1");
  t1.start();
  task1.printB();
 }
}
    
Output

main B
thread1 A
main B
thread1 A
main B
thread1 A
main B
thread1 A
main B
thread1 A

3. when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads

4. Constructors can't be synchronized

5. No need to apply synchronization on final fields. Since once the final variable initialized for an object, its value never change.


6. When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.

Synchronization                                                 Synchronized block                                                 Home

No comments:

Post a Comment