Static synchronization & non static synchronization

A synchronized method or block works on a given monitor. Synchronized non-static methods all synchronize on the Java instance of a class. Each instance has a lock monitor. For the case of static methods, what object does static synchronized methods use for locking? The static synchronized statements obtain a lock on the Class object.
If you make any static method as synchronized, the lock will be on the class not on object.

Static synchronization

Why we use Static Synchronized Block/Method?
Suppose there are two objects of a shared class(e.g. Account) 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.

The JVM creates a Class object when the class is loaded (When it is used for the first time). Once a class is loaded into a JVM, the same class will not be loaded again. The JVM creates one instance of Class for each class that is loaded, The Class instances are Objects and can be synchronized via static synchronized methods.

For example

class MyClass  {
  ...
  public synchronized static someMethod() {
    ...
  }
  ...
}

It is the equivalent to the following static synchronized block:

synchronized ( MyClass.class ) {
...
}

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

class Account{

 synchronized static void showAccount(String accountName){
     System.out.println("My account name is "+accountName+" Holder Name is "+Thread.currentThread().getName());
     try{
       Thread.sleep(500);
     }catch(Exception e){}
   }
}

class MyThread1 extends Thread{
    public void run(){
       Account.showAccount("Dineshonjava.com");
  }
}

class MyThread2 extends Thread{
    public void run(){
       Account.showAccount("Linkedin.com");
    }
}

class MyThread3 extends Thread{
    public void run(){
       Account.showAccount("Facebook.com");
    }
}

class MyThread4 extends Thread{
    public void run(){
       Account.showAccount("Twitter.com");
    }
}

class StaticSyncDemo{
    public static void main(String t[]){
       MyThread1 t1 = new MyThread1();
       MyThread2 t2 = new MyThread2();
       MyThread3 t3 = new MyThread3();
       MyThread4 t4 = new MyThread4();
       t1.setName("DAV JavaServices");
       t2.setName("dinesh.rajput");
       t3.setName("dineshonjava");
       t4.setName("admin@dineshonjava.com");       
       t1.start();
       t2.start();
       t3.start();
       t4.start();
    }
}

output:

non static synchronization

Synchronized block on a class lock:
The block synchronizes on the lock of the object denoted by the reference .class. A static synchronized method showAccount(String accountName) in class Account is equivalent to the following declaration:

static void showAccount(String accountName) {
    synchronized (Account.class) {       // Synchronized block on class A
        // ...
    }
}

<<Previous <<   || Index ||   >>Next >>
Previous
Next