All for Joomla All for Webmasters

Synchronization in Java

Synchronization in java is the capability to 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

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. void printTable(int n){//method not synchronized
  3.    for(int i=1;i<=5;i++){
  4.      System.out.println(n*i);
  5.      try{
  6.       Thread.sleep(400);
  7.      }catch(Exception e){System.out.println(e);}
  8.    }
  9.  }
  10. }
  11. class MyThread1 extends Thread{
  12. Table t;
  13. MyThread1(Table t){
  14. this.t=t;
  15. }
  16. public void run(){
  17. t.printTable(5);
  18. }
  19. }
  20. class MyThread2 extends Thread{
  21. Table t;
  22. MyThread2(Table t){
  23. this.t=t;
  24. }
  25. public void run(){
  26. t.printTable(100);
  27. }
  28. }
  29. class TestSynchronization1{
  30. public static void main(String args[]){
  31. Table obj = new Table();//only one object
  32. MyThread1 t1=new MyThread1(obj);
  33. MyThread2 t2=new MyThread2(obj);
  34. t1.start();
  35. t2.start();
  36. }
  37. }
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. class MyThread1 extends Thread{
  13. Table t;
  14. MyThread1(Table t){
  15. this.t=t;
  16. }
  17. public void run(){
  18. t.printTable(5);
  19. }
  20. }
  21. class MyThread2 extends Thread{
  22. Table t;
  23. MyThread2(Table t){
  24. this.t=t;
  25. }
  26. public void run(){
  27. t.printTable(100);
  28. }
  29. }
  30. public class TestSynchronization2{
  31. public static void main(String args[]){
  32. Table obj = new Table();//only one object
  33. MyThread1 t1=new MyThread1(obj);
  34. MyThread2 t2=new MyThread2(obj);
  35. t1.start();
  36. t2.start();
  37. }
  38. }
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
       

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. public class TestSynchronization3{
  13. public static void main(String args[]){
  14. final Table obj = new Table();//only one object
  15. Thread t1=new Thread(){
  16. public void run(){
  17. obj.printTable(5);
  18. }
  19. };
  20. Thread t2=new Thread(){
  21. public void run(){
  22. obj.printTable(100);
  23. }
  24. };
  25. t1.start();
  26. t2.start();
  27. }
  28. }
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
PinIt
submit to reddit

Leave a Reply

Top