Answer includes about what is synchronization in java multithreading and when do we use it or why it is important. Synchronized meaning, concept and program example explained with or without synchronization. This concept will help understand why to use thread synchronization in java multithreading.
Answer:
What is synchronization in java?
Synchronization in java multithreading is used if multiple threads share common resources. Resources can be a file IO, shared memory, a piece of code or methods etc. to avoid data integrity and corrupted data etc.
Also, synchronization is used for inter thread communication in java multithreading applications. For example, let’s say one thread is printing odd number and another thread is printing even number. Condition is that first thread print odd then second thread print even number. So, if we don’t use synchronisation in java threads then we don’t have control on threads and expected output cannot be there. As a solution. We can start odd thread first, print the odd value and notify second thread to print even number and wait. Now even thread will also do the same. This is example of inter thread compunction in java programming.
Examples for need of synchronization
Example-1: When a thread changes the state of a shared resource for example a variable, then changes may not become apparent to other threads without proper use of synchronization in Java programs if they are also accessing it. This means that one thread can’t be reading while another update the shared variable or resource etc. and need to use synchronized java code.
Example-2: Let’s say multiple threads are accessing the same file and reading and writing into it. if one or multiple threads are writing text into the file and others are reading. We may not get expected result and will see some mixed text into the file or reading result could be different than what we are expecting.
Java thread program example – with or without synchronization
Let’s see how a program behaves if two threads share and call the same function simultaneously through a program.
What below complete java thread program is doing?
In this program two threads t1 and t2 are accessing a common function incrementCount() of a class “Printer”
First, you can have a quick look on the complete program given below that has used synchronized method. Then continue reading from here
Below is the output of the program and method used in then program without synchronizing the method and with synchronization.
Before synchronization
Non-synchronized method used in Printer class:
public void incrementCount()
Output: This output can be different for different run depending upon thread scheduling
Thread-0
Thread-1
Current Count:0
Current Count:0
Incremented Count:2
Incremented Count:1
After synchronization
Java synchronized method used in Printer class:
public synchronized void incrementCount()
Output
Thread-0
Current Count:0
Incremented Count:1
Thread-1
Current Count:1
Incremented Count:2
Thread-1
Current Count:2
Incremented Count:3
What is difference between both outputs?
From method give below, the expected output is to first, get thread name, print current count, increment the count and then print incremented count. This sequence must be printed on console by each thread.
Here is the snips of the function incrementCount() defined in the class Printer
public synchronized void incrementCount()
{
System.out.println(Thread.currentThread().getName());
System.out.println("Current Count:"+ count);
count++;
System.out.println("Incremented Count:"+ count);
}
If you look at the outputs for both, in case of method is not synchronized then the output is not as expected. But in case of synchronized method the output is as expected.
Complete synchronized java multithreading program:
class Printer implements Runnable{
int count;
public Printer(int count){
this.count = count;
}
public synchronized void incrementCount()
{
System.out.println(Thread.currentThread().getName());
System.out.println("Current Count:"+ count);
count++;
System.out.println("Incremented Count:"+ count);
}
@Override
public void run() {
while(count < 20){
incrementCount();
}
}
}
public class Test {
public static void main(String[] args) {
Printer print = new Printer(0);
Thread t1 = new Thread(print);
Thread t2 = new Thread(print);
t1.start();
t2.start();
}
}
There are multiple types of synchronization in java programming are available. For example, synchronized block in java thread, class level or static synchronization in java thread, also called java synchronized static method. Also, java synchronized object way is available i.e. If we want to put lock on object level.