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:

  1. <method modifier> synchronized <method signature> {
  2.     // synchronized code block
  3. }
<method modifier> synchronized <method signature> {
    // synchronized code block
}


The syntax for a synchronized statement is as follows:

  1. synchronized (expression) {
  2.     // synchronized code block
  3. }
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

  1. Person person= new Person();
  2. synchronized (person) {
  3.     System.out.println("Synchronized statement");
  4. }
Person person= new Person();
synchronized (person) {
    System.out.println("Synchronized statement");
}


Synchronized instance methods

  1. class BankAccount {
  2.     private double balance;
  3.     synchronized void withdraw(double amount) {
  4.         this.balance -= amount;
  5.     }
  6.     synchronized void deposit(double amount) {
  7.         this.balance += amount;
  8.     }
  9. }
class BankAccount {
    private double balance;
    synchronized void withdraw(double amount) {
        this.balance -= amount;
    }
    synchronized void deposit(double amount) {
        this.balance += amount;
    }
}


Synchronized static method

  1. class CounterExample {
  2.     private static int count;
  3.     static synchronized void increase() {
  4.         count++;
  5.     }
  6. }
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.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on