The synchronized keyword in Java controls access to shared resources among multiple threads. It guarantees that only one thread can executed a synchronized block or method at a time, preventing thread interference and ensuring memory consistency.
Usage
The synchronized keyword can be applied to methods or blocks within methods. When a method or block is declared as synchronized, a lock is obtained on the specified object, and other threads are blocked from accessing the synchronized code until the lock is released.
Syntax
Synchronized Method
public synchronized voidsynchronizedMethod(){// method code}제공
object: The object on which to synchronize. This is typically the current instance (this) or a specific object.
Examples
Example 1: Synchronized Method
publicclassCounter{private int count =0;public synchronized voidincrement(){
count++;}public int getCount(){return count;}publicstaticvoidmain(String[] args){
Counter counter =newCounter();// Create multiple threads to increment the counter
Thread t1 =newThread(()-> counter.increment());
Thread t2 =newThread(()-> counter.increment());
t1.start();
t2.start();try{
t1.join();
t2.join();}catch(InterruptedException e){
e.printStackTrace();}
System.out.println("Final count: "+ counter.getCount());}}제공
In this example, the increment method is synchronized, ensuring that only one thread can increment the counter at a time, preventing race conditions.
Example 2: Synchronized Block
publicclassSynchronizedBlockExample{private final Object lock =newObject();private int count =0;publicvoidincrement(){synchronized(lock){
count++;}}public int getCount(){return count;}publicstaticvoidmain(String[] args){
SynchronizedBlockExample example =newSynchronizedBlockExample();// Create multiple threads to increment the counter
Thread t1 =newThread(()-> example.increment());
Thread t2 =newThread(()-> example.increment());
t1.start();
t2.start();try{
t1.join();
t2.join();}catch(InterruptedException e){
e.printStackTrace();}
System.out.println("Final count: "+ example.getCount());}}제공
This example uses a synchronized block to control access to the count variable. The lock object ensures that only one thread can execute the synchronized block at a time.
Tips and Best Practices
Minimize Synchronized Code: Keep synchronized blocks as short as possible to reduce contention and improve performance.
Use Final Objects: When using synchronized blocks, use final objects as locks to ensure consistency.
Avoid Nested Synchronization: Nested synchronized blocks can lead to deadlocks. Avoid them or use them with caution.
Use Concurrent Collections: For complex synchronization, consider using concurrent collections from the java.util.concurrent package, such as ConcurrentHashMap, which provide built-in synchronization.
Volatile Keyword: For variables that are accessed by multiple threads but do not require complex synchronization, consider using the volatile keyword to ensure visibility.
Thread Safety: Always ensure that shared resources are properly synchronized to maintain thread safety and avoid data corruption.
Synchronized Static Methods: Synchronize static methods to enforce synchronized access at the class level, ensuring only one thread can execute the static method across all instances.
The
synchronizedkeyword in Java controls access to shared resources among multiple threads. It guarantees that only one thread can executed a synchronized block or method at a time, preventing thread interference and ensuring memory consistency.Usage
The
synchronizedkeyword can be applied to methods or blocks within methods. When a method or block is declared as synchronized, a lock is obtained on the specified object, and other threads are blocked from accessing the synchronized code until the lock is released.Syntax
Synchronized Method
Synchronized Block
object: The object on which to synchronize. This is typically the current instance (this) or a specific object.Examples
Example 1: Synchronized Method
In this example, the
incrementmethod is synchronized, ensuring that only one thread can increment the counter at a time, preventing race conditions.Example 2: Synchronized Block
This example uses a synchronized block to control access to the
countvariable. The lock object ensures that only one thread can execute the synchronized block at a time.Tips and Best Practices
java.util.concurrentpackage, such asConcurrentHashMap, which provide built-in synchronization.volatilekeyword to ensure visibility.