Synchronization in Java
- Zakee Ahmed
- Apr 9, 2020
- 2 min read
Introduction: The Synchronization in java is the capability to control the access of multiple applications/methods to any shared resource. Java Synchronization is a better option where we want to allow only one application/method to access the shared resource.
Example 1:
Let us consider a display as a resource for many parallel programs executing, with java program we can synchronize the order of access to this display by all the programs. so that no two programs would try to access the screen at the same time instant.
Example 2:
Let us consider a limited memory having space of occupying 2 seconds of multimedia content. Two applications viz. a recorder and a player, are using this memory resource concurrently. consider the following issues.
1. Player accessing memory when there was no file present
2. Recorder is recording content again, where the previous file was not being read by Player, thus cause overwriting of file.
3. Both Recorder and Player are trying to access memory at same instant.
these issues can be resolved by synchronization in Java as depicted in the following figure.

Inter-thread communication or Co-operation in Java
Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. The Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter in the same critical section to be executed. In Java it is implemented by following methods of Object class:
- wait()
- notify()
- notifyAll()
//Consider following Java Program without synchronization
class Memory
{
int MemitemNo;
public void Write(int M)
{
MemitemNo=M;
System.out.println("Write Memory Value:"+ MemitemNo);
}
publi void Read()
{
System.out.println("Read Mem Value:"+ MemitemNo+"\n\n");
}
}
class producer extends Thread
{
Memory o;
producer(Memory o)
{
this.o=o;
}
public void run()
{
int MemitemNo=0;
for(int i=0;i<5;i++)
{
o.Write(++MemitemNo);
}
}
}
class consumer extends Thread
{
Memory o;
consumer(Memory o)
{
this.o=o;
}
public void run()
{
for(int i=0;i<5;i++)
{
o.Read();
}
}
}
public class InterThreadComm {
public static void main(String[] args) {
Memory o=new Memory();
Thread t1=new producer(o);
Thread t2=new consumer(o);
t1.start();
t2.start();
}
}
Output

//Now Consider following Java Program with synchronization
class Memory
{
int MemitemNo;
boolean order=true;
public synchronized void Write(int M)
{
if(order==false)
{
try{
wait();
}catch(InterruptedException ie){}
}
MemitemNo=M;
order=false;
System.out.println("Write Memory Value:"+ MemitemNo);
notify();
}
public synchronized void Read()
{
if(order==true){
try{
wait();
}catch(InterruptedException ie){}
}
order=true;
System.out.println("Read Mem Value:"+ MemitemNo+"\n\n");
notify();
}
}
class producer extends Thread
{
Memory o;
producer(Memory o)
{ this.o=o;
}
public void run()
{
int MemitemNo=0;
for(int i=0;i<5;i++){
o.Write(++MemitemNo);
}
}
}
class consumer extends Thread
{
Memory o;
consumer(Memory o){
this.o=o;
}
public void run()
{
for(int i=0;i<5;i++) {
o.Read();
}
}
}
public class InterThreadComm {
public static void main(String[] args) {
Memory o=new Memory();
Thread t1=new producer(o);
/*creating object of thread class initialized with producer class with a parameter of an object of memory class*/
Thread t2=new consumer(o);
/*creating object of thread class initialized with consumer class with a parameter of an object of memory class*/
t1.start();
t2.start();
}
}
Output

comparing cited two programs clearly justifies the use of Java synchronization for inter-thread communication using the specialized keyword synchronized and methods wait() and notify().
Comments