Thursday, 16 January 2014

Synchronization


Synchronization

Synchronization is the capabilility of control the access of multiple threads to any shared resource. Synchronization is better in case we want only one thread can access the shared resource at a time.

Why use Synchronization?

The synchronization is mainly used to
  1. To prevent thread interference.
  2. To prevent consistency problem.


Types of Synchronization

There are two types of synchronization
  1. Process Synchronization
  2. Thread Synchronization
Here, we will discuss only thread synchronization.

Thread Synchronization

There are two types of thread synchronization mutual exclusive and inter-thread communication.
  • Mutual Exclusive
    1. Synchronized method.
    2. Synchronized block.
    3. static synchronization.
  • Cooperation (Inter-thread communication)

Mutual Exclusive

Mutual Exclusive helps keep threads from interfering with one another while sharing data. This can be done by three ways in java:
  1. by synchronized method
  2. by synchronized block
  3. by static synchronization

Understanding the concept of Lock

Synchronization is built around an internal entity known as the lock or monitor.Every object has an lock associated with it. By convention, a thread that needs consistent access to an object's fields has to acquire the object's lock before accessing them, and then release the lock when it's done with them.
From Java 5 the package java.util.concurrent.locks contains several lock implementations.

Understanding the problem without Synchronization

In this example, there is no synchronization, so output is inconsistent. Let's see the example:
  1. Class Table{  
  2.   
  3. void printTable(int n){//method not synchronized  
  4.    for(int i=1;i<=5;i++){  
  5.      System.out.println(n*i);  
  6.      try{  
  7.       Thread.sleep(400);  
  8.      }catch(Exception e){System.out.println(e);}  
  9.    }  
  10.   
  11.  }  
  12. }  
  13.   
  14. class MyThread1 extends Thread{  
  15. Table t;  
  16. MyThread1(Table t){  
  17. this.t=t;  
  18. }  
  19. public void run(){  
  20. t.printTable(5);  
  21. }  
  22.   
  23. }  
  24. class MyThread2 extends Thread{  
  25. Table t;  
  26. MyThread2(Table t){  
  27. this.t=t;  
  28. }  
  29. public void run(){  
  30. t.printTable(100);  
  31. }  
  32. }  
  33.   
  34. class Use{  
  35. public static void main(String args[]){  
  36. Table obj = new Table();//only one object  
  37. MyThread1 t1=new MyThread1(obj);  
  38. MyThread2 t2=new MyThread2(obj);  
  39. t1.start();  
  40. t2.start();  
  41. }  
  42. }  
Output: 5
       100
       10
       200
       15
       300
       20
       400
       25
       500
       

Solution by synchronized method

  • If you declare any method as synchronized, it is known as synchronized method.
  • Synchronized method is used to lock an object for any shared resource.
  • When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the method returns.
  1. <b><i>//Program of synchronized method</i></b>  
  2.   
  3. Class Table{  
  4.   
  5.  synchronized void printTable(int n){//synchronized method  
  6.    for(int i=1;i<=5;i++){  
  7.      System.out.println(n*i);  
  8.      try{  
  9.       Thread.sleep(400);  
  10.      }catch(Exception e){System.out.println(e);}  
  11.    }  
  12.   
  13.  }  
  14. }  
  15.   
  16. class MyThread1 extends Thread{  
  17. Table t;  
  18. MyThread1(Table t){  
  19. this.t=t;  
  20. }  
  21. public void run(){  
  22. t.printTable(5);  
  23. }  
  24.   
  25. }  
  26. class MyThread2 extends Thread{  
  27. Table t;  
  28. MyThread2(Table t){  
  29. this.t=t;  
  30. }  
  31. public void run(){  
  32. t.printTable(100);  
  33. }  
  34. }  
  35.   
  36. class Use{  
  37. public static void main(String args[]){  
  38. Table obj = new Table();//only one object  
  39. MyThread1 t1=new MyThread1(obj);  
  40. MyThread2 t2=new MyThread2(obj);  
  41. t1.start();  
  42. t2.start();  
  43. }  
  44. }  
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
       

Same Example of synchronized method by using annonymous class

In this program, we have created the two threads by annonymous class, so less coding is required.
  1. <b><i>//Program of synchronized method by using annonymous class</i></b>  
  2.   
  3. Class Table{  
  4.   
  5.  synchronized void printTable(int n){//synchronized method  
  6.    for(int i=1;i<=5;i++){  
  7.      System.out.println(n*i);  
  8.      try{  
  9.       Thread.sleep(400);  
  10.      }catch(Exception e){System.out.println(e);}  
  11.    }  
  12.   
  13.  }  
  14. }  
  15.   
  16. class Use{  
  17. public static void main(String args[]){  
  18. final Table obj = new Table();//only one object  
  19.   
  20. MyThread1 t1=new MyThread1(){  
  21. public void run(){  
  22. obj.printTable(5);  
  23. }  
  24. };  
  25. MyThread1 t2=new MyThread1(){  
  26. public void run(){  
  27. obj.printTable(100);  
  28. }  
  29. };  
  30.   
  31. t1.start();  
  32. t2.start();  
  33. }  
  34. }  
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
       
- See more at: file:///C:/Users/vijay/Desktop/JavaTpoint/JavaTpoint/www.javatpoint.com/synchronization.html#sthash.zk7NZbB8.dpuf

No comments:

Post a Comment