Java Multithreading: Wait and Notify
A tutorial on wait and notify in Java; low-level
multithreading methods of the Object class that allow you to have one or more
threads sleeping, only to be woken up by other threads at the right moment.
Extremely useful for avoiding those processor-consuming polling loops.
Code For This Tutorial
The main program just runs the produce() and consume()
methods in different threads.
import
java.util.Scanner;
class Processor {
public void produce() throws InterruptedException
{
synchronized (this) {
System.out.println("Producer
thread running ....");
wait();
System.out.println("Resumed.");
}
}
public void consume() throws
InterruptedException {
Scanner scanner = new Scanner(System.in);
Thread.sleep(2000);
synchronized (this) {
System.out.println("Waiting
for return key.");
scanner.nextLine();
System.out.println("Return
key pressed.");
notify();
Thread.sleep(5000);
}
}
}
output:
Producer thread running ....
Waiting for return key.
Return key pressed.
Resumed.
The main program
(just creates and runs two threads):
public class App {
public static void main(String[]
args) throws InterruptedException {
final Processor
processor = new Processor();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.produce();
} catch
(InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.consume();
} catch
(InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
ReplyDeleteJava Training in Electronic City