Static Synchronization in Java
Last Updated :
25 Jun, 2024
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.
Important Points Regarding Synchronization
- 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.
- Synchronization is the most dangerous word in Java because this is the only reason for the deadlock.
- Use synchronized keywords when it is required and try to use synchronized blocks.
Static Synchronization
The Synchronized method may lose its behaviour of getting an ordered output. When there are more objects of a class, It acquires only the lock of the particular instance. To maintain the Synchronized behavior, we need a class-level lock rather than an instance-level lock which can be achieved by Static Synchronization.
Static Synchronized method is also a method of synchronizing a method in Java such that no two threads can act simultaneously static upon the synchronized method. The only difference is by using Static Synchronized. We are attaining a class-level lock such that only one thread will operate on the method. The Thread will acquire a class-level lock of a Java class, such that only one thread can act on the static synchronized method.
Syntax:
synchronized static return type class name{}
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

Threads and Classes
Here t1,t2... t6 are the thread names
The complete declarations of methods are:
method1: public static synchronized void method1()
method2: public static synchronized void method2()
method3: public static void method3()
method4: public synchronized int method4()
method5: public String method5()
- t1.method1() starts execution as it attains class level lock of Manager class.
- t2.method1() 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.
- t3.method2() waits as it requires class level lock, so it must wait until t1 releases the lock.
- t4.method3() starts execution as it is static methods requires no lock
- t5.method4() starts execution as it is instance or(normal) level synchronized method and requires object level lock, so it attains object level lock.
- t6.method5() starts execution as it is an instance method or a normal method
Example: Below is an example program of multithreading with static synchronized
Java
// Java program of multithreading
// with static synchronized
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 of the above Program:
In the above program, three classes, namely Display, MyThread, and Main, are defined where each class has - Â
- class Display: The code which needs for the child thread to run
- class MyThread: The purpose of this class is to extend class Thread and to allocate value names, and call the wish method of class Display
- class Main: It is the main class of the whole program, and it creates a child thread
The control flow:
As we know, the execution of the program starts with the main method. First, we create two child threads and assign them to the display objects for the threads, and after t2.start(), there will be three threads, namely(main,t1,t2), and the execution procedure is as follows.
The child threads start their execution t1, and as the wish method is static synchronized, the thread t1 gains the class level lock of class Display and starts executing the wish method. If the next thread comes, it must wait until the execution of the previous thread to attain the class level lock.
Note: We can't say the exact order of output. As a programmer, we cannot say which thread starts its execution or the order of execution ,they are not in the hands of a programmer it is the job of Thread schedular.
Difference between Synchronized and Static Synchronized in Java
 Synchronized | Static 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. |
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read