Static Synchronization in Java
Table of Contents
- Introduction
- Static Synchronization in Java
- Code Walkthrough
- Key Takeaways
- Conclusion
Introduction
In Java, synchronization is crucial when multiple threads attempt to access shared resources concurrently. This concept prevents thread interference and ensures thread safety. However, when you need to synchronize methods at the class level rather than the object level, static synchronization becomes essential.
This article explores static synchronization in Java, its usage, and how it works with a multi-threaded environment. We’ll delve into the code from a Java project to understand how to implement static synchronization effectively.
Static Synchronization in Java
What is Static Synchronization?
In Java, synchronization allows you to control the access of multiple threads to shared resources. Static synchronization refers to synchronizing static methods, meaning these methods are class-level and not object-level.
When to Use Static Synchronization?
- When you want to ensure that only one thread can access the synchronized static method across all instances of the class.
- If the resource being accessed is shared among all instances of a class.
Comparison Table
Synchronized Method | Static Synchronized Method |
---|---|
Object-level locking | Class-level locking |
Each object has its lock | One lock for the entire class |
Suitable for instance methods | Suitable for static (class) methods |
Code Walkthrough
Let’s examine a simple program that demonstrates static synchronization in Java. The code uses static synchronization to print brackets [] using multiple threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.studyeasy; class Brackets { synchronized public static void generate() { for (int i = 1; i <= 20; i++) { if (i <= 10) { System.out.print('['); } else { System.out.print(']'); } } System.out.println(); } } public class Main { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { Brackets.generate(); } } }).start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { Brackets.generate(); } } }).start(); } } |
Code Breakdown
1. Brackets Class:
The method generate() is declared as synchronized and static. This ensures that the method is synchronized at the class level. It prints 10 opening brackets [ followed by 10 closing brackets ].
2. Main Class:
Two threads are created, each calling the generate() method five times. Since the generate() method is statically synchronized, only one thread will be able to execute it at a time, even though two threads are trying to run it concurrently.
Output
1 2 3 4 5 6 7 8 9 10 |
[[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] [[[[[[[[[[]]]]]]]]]] |
Key Takeaways
- Static Synchronization ensures that only one thread can execute a static synchronized method at a time.
- It locks the class itself, not the individual objects, making it ideal for scenarios where shared resources are static.
- This concept is beneficial when dealing with multi-threaded programs where shared resources need to be protected from concurrent access.
Conclusion
Static synchronization is a powerful tool in Java for managing thread safety at the class level. Understanding when and how to use it can prevent potential race conditions and ensure your program runs smoothly in a multi-threaded environment. By synchronizing static methods, you provide a class-level lock, ensuring that only one thread can access the synchronized method at any given time.