Static Synchronization in JavaLast Updated : 20 Jan 2026 Java static synchronization ensures thread safety for shared static resources by locking the class-level object instead of individual instances. In this chapter, we will learn how static synchronization works and how it ensures safe access to shared static data in a multithreaded environment. What Is Static Synchronization?Although a synchronized method ensures that only one thread can access it at a time, it applies an instance-level lock, which may not be sufficient when multiple objects or static members are involved. In such cases, the execution order of threads is not guaranteed across different instances. Static synchronization solves this problem by applying a class-level lock instead of an object-level lock. When a static method is declared as synchronized, the lock is placed on the Class object, ensuring that only one thread can execute any static synchronized method of that class at a time. For an example, if a class contains multiple static synchronized methods (such as demo1, demo2, demo3, and demo4), and one thread is executing demo1, no other thread can access any of the other static synchronized methods simultaneously. This guarantees consistent behavior for shared static resources. Creating Static Synchronization BlockStatic synchronization is achieved by applying a lock at the class level instead of the object level. While static synchronized methods automatically lock the class, you can also explicitly create a static synchronized block by locking the Class object. SyntaxHere is the syntax: Why Static Synchronization Is Needed?Suppose there are two objects of a shared class Table, named object1 and object2. When using synchronized methods or synchronized blocks, threads working on the same object (for example, t1 and t2) do not interfere with each other because they share a single object-level lock. However, threads operating on different objects (such as t1 with object1 and t3 with object2) can still interfere with each other because each object has its own lock. This means synchronization at the instance level is not sufficient to protect shared static resources. To avoid such interference across multiple objects, static synchronization is used. It applies a class-level lock, ensuring that only one thread can access the shared resource at a time, regardless of how many objects of the class are created. Therefore, static synchronization is preferred when a shared resource must be accessed in a thread-safe and consistent manner across all instances of a class. Example of Static SynchronizationThe following example demonstrates how static synchronization applies a class-level lock to ensure that only one thread executes the method at a time, even when multiple objects are created. ExampleCompile and RunOutput: Good Night: Mahi Good Night: Mahi Good Night: Mahi Good Night: Mahi Good Night: Sachin Good Night: Sachin Good Night: Sachin Good Night: Sachin Good Night: Mahi Good Night: Mahi Good Night: Mahi Good Night: Mahi Good Night: Sachin Good Night: Sachin Good Night: Sachin Good Night: Sachin Explanation: In this program, Print contains a static synchronized method, MyThrd creates and runs threads that call this method, and Main starts multiple threads. Since the method is static synchronized, the first thread acquires the class-level lock and executes the method, while the other thread waits until the lock is released, ensuring thread-safe and ordered execution. Static Synchronization by Using the Anonymous ClassIn static synchronization using an anonymous class, threads are created without defining separate thread classes, while synchronization is still maintained at the class level. Since the method is static synchronized, all threads share the same class-level lock, ensuring that only one thread executes the method at a time. ExampleIn this example, multiple threads are created using anonymous classes, and static synchronization ensures that each thread executes the printTable() method one after another without interference.Output: 100 200 300 400 500 600 700 800 900 1000 10 20 30 40 50 60 70 80 90 100 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 1 2 3 4 5 6 7 8 9 10 Synchronized Block on a Class LockA synchronized block on a class lock is used to achieve static synchronization without declaring the entire method as static synchronized. In this approach, the lock is acquired on the ClassName.class object, which represents the class-level monitor. This ensures that only one thread can execute the synchronized block across all instances of the class. A static synchronized method is internally equivalent to a synchronized block that locks the class object. ExampleIn the following example, a synchronized block locks the Table.class object to provide class-level synchronization for a shared static resource. Output: 5 10 15 20 25 100 200 300 400 500 Difference Between Synchronization and Static SynchronizationThe following table shows the difference between synchronization and static synchronization.
Next TopicDeadlock In Java |
We request you to subscribe our newsletter for upcoming updates.