Synchronization

PREVIOUS

Synchronization in Java

Synchronization in java is the capability of control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread to access the shared resource.

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.
  1. Mutual Exclusive
    1. Synchronized method.
    2. Synchronized block.
    3. static synchronization.
  2. Cooperation (Inter-thread communication in java)

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 in Java

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 TestSynchronization1{  
  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
       

Java 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 thread completes its task.
  1. //example of java synchronized method  
  2. class Table{  
  3.  synchronized void printTable(int n){//synchronized method  
  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. public class TestSynchronization2{  
  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
       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. //Program of synchronized method by using annonymous class  
  2. class Table{  
  3.  synchronized void printTable(int n){//synchronized method  
  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. public class TestSynchronization3{  
  15. public static void main(String args[]){  
  16. final Table obj = new Table();//only one object  
  17.   
  18. Thread t1=new Thread(){  
  19. public void run(){  
  20. obj.printTable(5);  
  21. }  
  22. };  
  23. Thread t2=new Thread(){  
  24. public void run(){  
  25. obj.printTable(100);  
  26. }  
  27. };  
  28.   
  29. t1.start();  
  30. t2.start();  
  31. }  
  32. }  
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500

No comments:

Post a Comment