What is Synchronization?
- Synchronization in the Thread is the ability to control multiple threads’ access over any shared resource.
- We have seen what multithreading is where multiple threads execute at the same time, in this time if 2 or more threads try to access any resource we will get inconsistent behavior.
- For example, writing into any file, if we have 3 threads head Thread, body Thread, and footer thread, if all the threads start writing into the file at the same time then we will get the random output written in the file. To restrict that we need to have synchronization of the threads.
How to achieve Synchronization in Thread?
- If the multiple threads are trying to operate simultaneously on the same java object there may be a chance of a Data inconsistency problem. To overcome this problem we should go for synchronized keywords.
- synchronized is only for methods and blocks.
- At a time only one thread is allowed to execute that method or block on the given object so that the Data inconsistency problem will be resolved.
- Note that when an object is locked by a thread then another thread can not access its any synchronized method/block but normal methods can be accessible.
Synchronized Methods:
Let’s see the following example.
public static void main(String args[]) throws InterruptedException {
Greet g = new Greet();
MyThread t1 = new MyThread(g, “ugtworld”);
MyThread t2 = new MyThread(g, “abcd”);
t1.start();
t2.start();
}
}
class MyThread extends Thread {
Greet greet;
String name;
public MyThread(Greet greet, String name) {
this.greet = greet;
this.name = name;
}
public void run() {
greet.message(name);
}
}
class Greet {
public synchronized void message(String name) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(“Hello ” + name);
}
}
}
Hello ugtworld
Hello ugtworld
Hello ugtworld
Hello ugtworld
Hello abcd
Hello abcd
Hello abcd
Hello abcd
Hello abcd
- If in the above example if the message method of the greet class is not synchronized then we will get a mixed output.
- For the synchronized method, we will get the synchronized output.
- Note that in the above example two different threads are operating on the same greet object ‘g’. If these two threads operate on two different Greet class objects then we will get the mixed output, even though we have a synchronized massage method.
- If our method in the Greet class is ‘static synchronized’ and different threads operating on different objects, we will get the regular synchronized output as shown in the example since the lock on this method will be on the class level.
- Class level locks are for static methods only.
- Every class in Java has a unique lock which is nothing but the class level lock for static synchronized methods.
Synchronized blocks:
- If very few lines of the code required synchronization then it is not required to declare an entire method as synchronized. We have to enclose those few lines of code by using a synchronized block.
- This reduces the waiting time of the Threads and improves the performance.
- We can declare synchronize blocks as follows.
- To get lock of current object: If a thread got the lock of the current object then only it is allowed to execute this area.
syntax:
Example
public static void main(String args[]) throws InterruptedException {
Greet g = new Greet();
MyThread t1 = new MyThread(g, “ugtworld”);
MyThread t2 = new MyThread(g, “abcd”);
t1.start();
t2.start();
}
}
class MyThread extends Thread {
Greet greet;
String name;
public MyThread(Greet greet, String name) {
this.greet = greet;
this.name = name;
}
public void run() {
greet.message(name);
}
}
class Greet {
public void message(String name) {
for (int i = 0; i < 5; i++) {
synchronized(this) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(“Hello ” + name);
}
}
}
}
- To get a lock of a particular object : If a thread got a lock of a particular object ‘a’ then only it is allowed to execute this area.
Syntax:
Example
public static void main(String args[]) throws InterruptedException {
Greet g = new Greet();
MyThread t1 = new MyThread(g, “ugtworld”);
MyThread t2 = new MyThread(g, “abcd”);
t1.start();
t2.start();
}
}
class MyThread extends Thread {
Greet greet;
String name;
public MyThread(Greet greet, String name) {
this.greet = greet;
this.name = name;
}
public void run() {
greet.message(name);
}
}
class Greet {
String obj = “lockObject”;
public void message(String name) {
for (int i = 0; i < 5; i++) {
synchronized(obj) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(“Hello ” + name);
}
}
}
}
- To get class level lock:
If a thread got the class level lock of the Greet class, then only it is allowed to execute this area. In this case, even if two threads are operating on two different objects of the Greet, since the lock is on class level, we will get the synchronized output.
Syntax:
Example
public static void main(String args[]) throws InterruptedException {
Greet g = new Greet();
MyThread t1 = new MyThread(g, “ugtworld”);
MyThread t2 = new MyThread(g, “abcd”);
t1.start();
t2.start();
}
}
class MyThread extends Thread {
Greet greet;
String name;
public MyThread(Greet greet, String name) {
this.greet = greet;
this.name = name;
}
public void run() {
greet.message(name);
}
}
class Greet {
public void message(String name) {
for (int i = 0; i < 5; i++) {
synchronized(Greet.class) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(“Hello ” + name);
}
}
}
}
What is Race condition?
Race Condition occurs when multiple threads operate simultaneously on the same java object causing a Data inconsistency problem. This can be overcome by using synchronized keyword.
What is synchronized statement?
A statement present in the synchronized methods or blocks is called a synchronized statement.