SYNCHRONIZATION

 

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.

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

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. It can be achieved by using the following three ways:

  1. By Using Synchronized Method
  2. By Using Synchronized Block
  3. By Using Static Synchronization

Concept of Lock in Java

Synchronization is built around an internal entity known as the lock or monitor. Every object has a 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.

  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. }  
  12.   
  13. class MyThread1 extends Thread{  
  14. Table t;  
  15. MyThread1(Table t){  
  16. this.t=t;  
  17. }  
  18. public void run(){  
  19. t.printTable(5);  
  20. }  
  21.   
  22. }  
  23. class MyThread2 extends Thread{  
  24. Table t;  
  25. MyThread2(Table t){  
  26. this.t=t;  
  27. }  
  28. public void run(){  
  29. t.printTable(100);  
  30. }  
  31. }  
  32.   
  33. class TestSynchronization1{  
  34. public static void main(String args[]){  
  35. Table obj = new Table();//only one object  
  36. MyThread1 t1=new MyThread1(obj);  
  37. MyThread2 t2=new MyThread2(obj);  
  38. t1.start();  
  39. t2.start();  
  40. }  
  41. }  

output:
            5
            100
            10
            200
            15
            300
            25
            400
            20
            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
          
       10
       200
       15
       300
       20
       400
       25
       500
       100
       10
       200
       15
       300
       20
       400
       25
       500

Comments

Popular posts from this blog

Link building in SEO

NODE LOOK UP IN PEER-TO-PEER NETWORK

NORMALIZATION