synchronized keyword in java
Synchronization in java is used to control the access of multiple threads to any shared resource.
Synchronization is better option when we want to allow only one thread to access the shared resource.
"synchronized"
keyword is used to achieve Synchronization in Java.
The synchronized keyword can be applied to a method or statement block and provides protection for critical sections(synchronized block) that should only be executed by one thread at a time.
If we declare any method as synchronized
, it is called as synchronized method.
The syntax for a synchronized method is as follows:
- <method modifier> synchronized <method signature> {
- // synchronized code block
- }
<method modifier> synchronized <method signature> { // synchronized code block }
The syntax for a synchronized statement is as follows:
- synchronized (expression) {
- // synchronized code block
- }
synchronized (expression) { // synchronized code block }
The expression should be evaluated to a reference type(object reference).
How synchronization works?
Every object has a lock associated with it, any thread that needs access to an object’s fields has to acquire the object’s lock before accessing them and then release the lock when it completes the execution.
The current executing thread will always try to acquire a lock before executing the synchronized code.
For synchronized statement:
Thread acquires the lock associated with the object returned by the expression.
For synchronized method:
If the method is static, thread acquires the lock associated with the Class object of the class in which the method is declared.
If the method is non-static, thread acquires the lock associated with current object.
If the current executing thread owns the lock, no other threads can acquire that lock.
When the synchronized code block completes, the current executing thread releases the lock.
Examples
Synchronized
block
- Person person= new Person();
- synchronized (person) {
- System.out.println("Synchronized statement");
- }
Person person= new Person(); synchronized (person) { System.out.println("Synchronized statement"); }
Synchronized
instance methods
- class BankAccount {
- private double balance;
- synchronized void withdraw(double amount) {
- this.balance -= amount;
- }
- synchronized void deposit(double amount) {
- this.balance += amount;
- }
- }
class BankAccount { private double balance; synchronized void withdraw(double amount) { this.balance -= amount; } synchronized void deposit(double amount) { this.balance += amount; } }
Synchronized
static method
- class CounterExample {
- private static int count;
- static synchronized void increase() {
- count++;
- }
- }
class CounterExample { private static int count; static synchronized void increase() { count++; } }
When to use synchronization in java ?
Whenever we have some piece of code which needs to be executed sequentially by multiple threads rather than executing at the same time, we should use synchronization.