What is Synchronized keyword in Java

Synchronization in Java

Synchronization in java is used to control the access of multiple threads to any shared resource.

It allows only one thread to access the shared resource.

Why use Synchronization

The synchronization is mainly used to

  1. prevent thread interference.
  2. prevent consistency problem.

There are two types of thread synchronization mutual exclusive and inter-thread communication.

  1. Mutual Exclusive
    1. Synchronized method.
    2. Synchronized block.
    3. static synchronization.
  2. Inter-thread communication in java

In java Synchronization can be done by either synchronized method or synchronized block and we use synchronized keyword for both ways.

What is Lock in Java

Every object has a lock or monitor associated with it. Therefore, when thread enters into the synchronized block or method then monitor is acquired by that thread which has entered into the block , and then release the lock when it’s done with that block.

Problem without Synchronization

In this example, there is no synchronization, so output is inconsistent. Let’s see the example:

class Table {
void printTable(int n) { //method not synchronized 
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}

}
}

class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}

}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}

class TestSynchronization1 {
public static void main(String args[]) {
Table obj = new Table(); //only one object 
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}
 
Output: 5
       100
       10
       200
       15
       300
       20
       400
       25
       500

Java synchronized method

If you declare any method as synchronized, it is known as synchronized method.

Synchronized method is used to lock an object for any shared resource.

When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.

//example of java synchronized method 
class Table {
synchronized void printTable(int n) { //synchronized method 
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}

}
}

class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}

}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}

public class TestSynchronization2 {
public static void main(String args[]) {
Table obj = new Table(); //only one object 
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500

Synchronized block in java

Synchronized block can be used to perform synchronization on any specific resource of the method.

Suppose you have 100 lines of code in your method, but you want to synchronize only 20 lines, you can use synchronized block.

If you put all the codes of the method in the synchronized block, it will work same as the synchronized method.

Points to remember for Synchronized block

  • Synchronized block is used to lock an object for any shared resource.
  • Scope of synchronized block is smaller than the method.

Syntax to use synchronized block

  1. synchronized (object) {
  2.   //code block 
  3. }

Important Points:

  1. At a time only one thread can access a synchronized method or block.
  2. If any static method is synchronized then at a time only one object’s one thread can access the method. This is also called class level synchronization.

One Comment on “What is Synchronized keyword in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *