Synchronization in Java:
In mulch-threaded programming we must consider synchronization of commonly shared resources/operations, to improve performance and avoid weird behavior of programming to get expected result. Why it is important means, if a common resource is used by more than one thread, then there is possibility of changing of state/values of commonly shared resources when another thread also working on the same. So it will lead to unexpected behavior.
Let us see the example program which counts the numbers from 1-3000 and prints them. But it is called by two persons(threads) to count. We have two methods one is synchronized and another one is non-synchronized normal method.
Note : Difference between synchronizing method and block in a method.
1. If you are overriding a method which is not synchronized in its declaration then you cover the code part inside the method by using synchronized keyword so the part of the code becomes synchronized.
2. If you synchronize method it is not accessible for other till the thread release control, but if you have synchronized block then the others will get access to the method and only wait to execute particular lines of code only. This will improve performance of the whole system because many objects can acquire the system.
Output of Normal method : (public void incrementAndPrintInt) is shown below when two persons Thread_1 and Thread_2 called them.
Thread_1 0
Thread_2 0
Thread_1 1
Thread_2 2
Thread_2 4
Thread_2 5
Thread_2 6
Thread_1 3
Thread_2 7
Thread_1 8
Thread_2 9
Thread_1 10
Output of Synchronized method : (public synchronized void incrementAndPrintInt) is shown below when two persons Thread_1 and Thread_2 called them.
Thread_2 0
Thread_2 1
Thread_2 2
Thread_2 3
Thread_2 4
Thread_2 5
Thread_2 6
Thread_2 7
Thread_2 8
Thread_2 9
Thread_2 10
Explanation:
In the normal method it is accessed by both the persons and called so the count values is affected by both of them, ie., the values increased by Thread_1 is unknown to Thread_2. So Thread_1 missed to count 2,4,5,6,7. And the counting also not in order. This is a unwanted/unexpected behavior of the Counter.
But if you see the output of the Synchronized method then count is in order like 1,2,3,4....10. And Thread_1 is not interrupted the method while Thread_2 is counting. This is what we want. When one person is working with one resource don't let the others disturb. So here the Thread_2 locked the method and releases if after completing its job to another thread. During the lock period no one disturbs the counter.
Code 1 : Main Class instantiates counter and shares it to threas objects and triggers them
public class SynchronizedTest {
public static void main(String[] args) {
MyCounter c = new MyCounter();
Thread1 t1 = new Thread1(c);
Thread2 t2 = new Thread2(c);
t1.start();
t2.start();
}
}
Code 2: MyCounter class has method incrementAndPrintInt is given here as non-synchronized. But to see the output of synchronized please add synchronized keyword in the method signature and compare the difference.
public class MyCounter {
int count =0;
// put synchronized before void for output two
public void incrementAndPrintInt(String s) {
while (count < 1000) {
System.out.println(s+" "+count);
count++;
}
}
}
Code 3: Thread1 is a class calls the counter count method.
public class Thread1 extends Thread {
MyCounter counter;
Thread1(MyCounter counter) {
this.counter = counter;
}
public void run() {
counter.incrementAndPrintInt("Thread_1");
}
}
Code 4: Thread2 is a class calls the counter count method.
public class Thread2 extends Thread {
MyCounter counter;
Thread2(MyCounter counter) {
this.counter = counter;
}
public void run() {
counter.incrementAndPrintInt("Thread_2");
}
}