Java Multi threading

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.

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:

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago