All for Joomla All for Webmasters

Static synchronization

If you make any static method as synchronized, the lock will be on the class not on object.

Problem without static synchronization

Suppose there are two objects of a shared class(e.g. Table) named object1 and object2.In case of synchronized method and synchronized block there cannot be interference between t1 and t2 or t3 and t4 because t1 and t2 both refers to a common object that have a single lock.But there can be interference between t1 and t3 or t2 and t4 because t1 acquires another lock and t3 acquires another lock.I want no interference between t1 and t3 or t2 and t4.Static synchronization solves this problem.

Example of static synchronization

In this example we are applying synchronized keyword on the static method to perform static synchronization.

  1. class Table{
  2.  synchronized static void printTable(int n){
  3.    for(int i=1;i<=10;i++){
  4.      System.out.println(n*i);
  5.      try{
  6.        Thread.sleep(400);
  7.      }catch(Exception e){}
  8.    }
  9.  }
  10. }
  11. class MyThread1 extends Thread{
  12. public void run(){
  13. Table.printTable(1);
  14. }
  15. }
  16. class MyThread2 extends Thread{
  17. public void run(){
  18. Table.printTable(10);
  19. }
  20. }
  21. class MyThread3 extends Thread{
  22. public void run(){
  23. Table.printTable(100);
  24. }
  25. }
  26. class MyThread4 extends Thread{
  27. public void run(){
  28. Table.printTable(1000);
  29. }
  30. }
  31. public class TestSynchronization4{
  32. public static void main(String t[]){
  33. MyThread1 t1=new MyThread1();
  34. MyThread2 t2=new MyThread2();
  35. MyThread3 t3=new MyThread3();
  36. MyThread4 t4=new MyThread4();
  37. t1.start();
  38. t2.start();
  39. t3.start();
  40. t4.start();
  41. }
  42. }
Output: 1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       10       
       20
       30
       40
       50
       60
       70
       80
       90
       100
       100
       200
       300
       400
       500
       600
       700
       800
       900
       1000
       1000
       2000
       3000
       4000
       5000
       6000
       7000
       8000
       9000
       10000
       

Same example of static synchronization by annonymous class

In this example, we are using annonymous class to create the threads.

  1. class Table{
  2.  synchronized static  void printTable(int n){
  3.    for(int i=1;i<=10;i++){
  4.      System.out.println(n*i);
  5.      try{
  6.        Thread.sleep(400);
  7.      }catch(Exception e){}
  8.    }
  9.  }
  10. }
  11. public class TestSynchronization5 {
  12. public static void main(String[] args) {
  13.     Thread t1=new Thread(){
  14.         public void run(){
  15.             Table.printTable(1);
  16.         }
  17.     };
  18.     Thread t2=new Thread(){
  19.         public void run(){
  20.             Table.printTable(10);
  21.         }
  22.     };
  23.     Thread t3=new Thread(){
  24.         public void run(){
  25.             Table.printTable(100);
  26.         }
  27.     };
  28.     Thread t4=new Thread(){
  29.         public void run(){
  30.             Table.printTable(1000);
  31.         }
  32.     };
  33.     t1.start();
  34.     t2.start();
  35.     t3.start();
  36.     t4.start();
  37. }
  38. }
Output: 1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       10       
       20
       30
       40
       50
       60
       70
       80
       90
       100
       100
       200
       300
       400
       500
       600
       700
       800
       900
       1000
       1000
       2000
       3000
       4000
       5000
       6000
       7000
       8000
       9000
       10000
       

Synchronized block on a class lock:

The block synchronizes on the lock of the object denoted by the reference .class name .class. A static synchronized method printTable(int n) in class Table is equivalent to the following declaration:

  1. static void printTable(int n) {
  2.     synchronized (Table.class) {       // Synchronized block on class A
  3.         // …
  4.     }
  5. }
PinIt
submit to reddit

Leave a Reply

Top