Static Synchronization in Java

Last Updated : 17 Mar, 2026

Synchronization is the potential to regulate the access of multiple threads to any shared resource. Synchronization in Java is essential for reliable communication between threads. It is achieved in Java with the use of synchronized keywords.

  • It is only for methods that are at the Object level.
  • If a method or block is synchronized, then it requires an object-level lock to start execution.
  • Use synchronized keywords when it is required and try to use synchronized blocks.

Static Synchronization

Static synchronization in Java is used to control access to shared resources at the class level. It ensures that only one thread can execute a static synchronized method at a time, regardless of the number of objects.

  • Provides a class-level lock instead of object-level lock
  • Only one thread can execute the static synchronized method at a time
  • Maintains proper synchronization even when multiple objects are created

Syntax:

static synchronized void methodName() {
// code
}

Note: When a class has both synchronized and static synchronized methods they can run parallelly ,as those two methods require different locks.

Let us assume that there are 6 threads. The order of execution will be

Example - 6 Threads in Java

The complete declarations of methods are:

Here t1,t2... t6 are the thread names

  1. t1.method1(): public static synchronized void method1() . Starts execution as it attains class level lock of Manager class.
  2. t2.method1(): public static synchronized void method2().Wait for its time to start execution, as it is a static synchronized method, it requires a class level lock,  as t1 has already acquired class level lock t2 must wait until t1 execution.
  3. t3.method2() :public static void method3() .Waits as it requires class level lock, so it must wait until t1 releases the lock.
  4. t4.method3() : public synchronized int method4() .Starts execution as it is static methods requires no lock
  5. t5.method4() : public String method5()starts execution as it is instance or(normal) level synchronized method and requires object level lock, so it attains object level lock.
  6. t6.method5() :starts execution as it is an instance method or a normal method

Example: Java program of multithreading with static synchronized

Java
class Display
{
    public static synchronized void wish(String name)
    {
        for(int i=0;i<3;i++)
        {
            System.out.print("Good Morning: ");
            System.out.println(name);
            try{
                Thread.sleep(2000);
            }
            catch(InterruptedException e)
            {
            }
        }
    }
}

class MyThread extends Thread{
    Display d;
    String name;
    MyThread(Display d,String name)
    {
        this.d=d;
        this.name=name;
    }
    public void run()
    {
        d.wish(name);
    }
}

class Main{
    public static void main(String arg[])
    {
        Display d1=new Display();
        Display d2=new Display();
        MyThread t1=new MyThread(d1,"Dhoni");
        MyThread t2=new MyThread(d2,"Yuvaraj");
        t1.start();
        t2.start();
    }
}

Note: Each wish will be printed after a gap of 2000 ms.

Output

First time of execution:
Good Morning: Dhoni
Good Morning: Dhoni
Good Morning: Dhoni
Good Morning: Yuvaraj
Good Morning: Yuvaraj
Good Morning: Yuvaraj

Second time of execution:
Good Morning: Yuvaraj
Good Morning: Yuvaraj
Good Morning: Yuvaraj
Good Morning: Dhoni
Good Morning: Dhoni
Good Morning: Dhoni

Explanation

  • The wish() method is declared static synchronized, so it locks on the class-level lock and allows only one thread to execute it at a time.
  • Even though two different objects (d1 and d2) are used, both threads share the same class lock, so execution is sequential.
  • As a result, one thread completes printing all “Good Morning” messages before the other thread starts.

Difference between Synchronized and Static Synchronized in Java

 SynchronizedStatic Synchronized
It requires an object-level lock.It requires a class-level lock.
Its method need not be declared static.Its method needs to be declared static.
It is used regularly.It is not used regularly.
 A different instance is created for each object.Only one instance for the entire program.
Comment