The synchronized keyword may be applied to a method or statement block and provides protection for critical sections that should only be executed by one thread at a time.
We can synchronize our code in either of two ways. Both involve the use of the synchronized keyword.
- Using Synchronized Methods
- Using Synchronized Statement Or Block
1. Using Synchronized Methods
To make a method synchronized, simply add the synchronized keyword to its declaration:
public class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
}
If a count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:
- First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
- Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
2. Using Synchronized Statement or Block
You do not have to synchronize a whole method. Sometimes it is preferable to synchronize only part of a method. Java synchronized blocks inside methods make this possible.
Here is a synchronized block of Java code inside an unsynchronized Java method:
public void add(int value){
synchronized(this){
this.count += value;
}
}
This example uses the Java synchronized block construct to mark a block of code as synchronized. This code will now execute as if it was a synchronized method.
Summary
- The synchronized keyword prevents a critical section of code from being executed by more than one thread at a time.
- When applied to a static method, the entire class is locked while the method is being executed by one thread at a time.
- When applied to an instance method, the instance is locked while being accessed by one thread at a time.
- When applied to an object or array, the object or array is locked while the associated code block is executed by one thread at a time.
Read more at Synchronization in Multithreading Java
All Java Keywords
- abstract Java Keyword
- assert Java Keyword
- boolean Java Keyword
- break Java Keyword
- byte Java Keyword
- case Java Keyword
- catch Java Keyword
- char Java Keyword
- class Java Keyword
- continue Java Keyword
- default Java Keyword
- do Java Keyword
- double Java Keyword
- else Java Keyword
- enum Java Keyword
- extends Java Keyword
- final Java Keyword
- finally Java Keyword
- float Java Keyword
- for Java Keyword
- if Java Keyword
- implements Java Keyword
- import Java Keyword
- instanceof Java Keyword
- int Java Keyword
- interface Java Keyword
- long Java Keyword
- native Java Keyword
- new Java Keyword
- package Java Keyword
- private Java Keyword
- protected Java Keyword
- public Java Keyword
- return Java Keyword
- short Java Keyword
- static Java Keyword
- strictfp Java Keyword
- super Java Keyword
- switch Java Keyword
- synchronized Java Keyword
- this Java Keyword
- throw Java Keyword
- throws Java Keyword
- transient Java Keyword
- try Java Keyword
- void Java Keyword
- volatile Java Keyword
- while Java Keyword
Comments
Post a Comment
Leave Comment