Static Synchronization in Java - GeeksforGeeks

03 Dec, 2021
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 block.
The Synchronized method may lose its behavior 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
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.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.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 lockt5.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
t1.method1() starts execution as it attains class level lock of Manager class.
t2.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.
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:
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 runclass MyThread: The purpose of this class is to extend class Thread and to allocate value names, and call the wish method of class Displayclass Main: It is the main class of the whole program, and it creates a child thread
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.
Java-Object Oriented
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
HashMap containsKey() Method in Java
How to Create Array of Objects in Java?
Convert Double to Integer in Java
Iterating over ArrayLists in Java
Generating random numbers in Java