Java has a mechanism called inter-thread communication that allows another thread to enter (or lock) a paused thread’s critical section so that it can be executed.
Polling is the process of repeatedly testing a condition until it holds true. Loops are frequently used to implement polling in order to determine whether a specific condition is true or false. If it is real, a particular thing happens. Due to this, the implementation uses up a lot of CPU cycles inefficiently.
Java uses the wait(), notify(), and notifyAll() methods to avoid polling. All of these methods are final members of the object class, making them available to all classes. They can only be used inside a synchronized block.
Let’s examine the key distinctions between the wait and sleep methods.
wait() | sleep() |
---|---|
The lock is released by the wait() method. | The lock is not released by the sleep() method. |
It is a method of the class Object. | It is a thread class method. |
Furthermore, it is the non-static method | It is the static method |
It should be notified by notify() or notifyAll() methods | After the specified amount of time, sleep is completed. |
//In this program, we will understand how to use wait and notify.
// It is the most efficient way for thread communication.
public class A
{
int i;
boolean flag = false; // flag will be true when data production is over.
synchronized void deliver(int i)
{
if(flag)
try
{
wait(); // Wait till a notification is received from Thread2. There will be no wastage of time.
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
this.i = i;
flag = true; // When data production is over, it will store true into flag.
System.out.println("Data Delivered: " +i);
notify(); // When data production is over, it will notify Thread2 to use it.
}
synchronized int receive()
{
if(!flag)
try {
wait(); // Wait till a notification is received from Thread1.
}
catch(InterruptedException ie){
System.out.println(ie);
}
System.out.println("Data Received: " + I);
flag = false; // It will store false into flag when data is received.
notify(); // When data received is over, it will notify Thread1 to produce next data.
return i;
}
}
public class Thread1 extends Thread
{
A obj;
Thread1(A obj)
{
this.obj = obj;
}
public void run()
{
for(int j = 1; j <= 5; j++){
obj.deliver(j);
}
}}
public class Thread2 extends Thread
{
A obj;
Thread2(A obj)
{
this.obj = obj;
}
public void run()
{
for(int k = 0; k <= 5; k++){
obj.receive();
}
}}
public class Communication
{
public static void main(String[] args)
{
A obj = new A(); // Creating an object of class A.
// Creating two thread objects and pass reference variable obj as parameter to Thread1 and Thread2.
Thread1 t1 = new Thread1(obj);
Thread2 t2 = new Thread2(obj);
// Run both threads.
t1.start();
t2.start();
}
}
Data Delivered: 1
Data Received: 1
Data Delivered: 2
Data Received: 2
Data Delivered: 3
Data Received: 3
Data Delivered: 4
Data Received: 4
Data Delivered: 5
Data Received: 5
In the event that the flag reads false, Thread2 will wait for the object until it is notified by a notify() method.
When the data production is finished, Thread1 will immediately notify Thread2 that data is available for receipt. Thread1 and Thread2 effectively communicate with one another in this way.
Note: also read about the Synchronization in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…