==
package Demo10;
/**
* Title -- Advanced Java_ Multi-threading Part 8 - Wait and Notify
*
* @author Dharmaraj.Net
*/
public class App {
public static void main(String[] args) throws InterruptedException {
final Processor processor = new Processor();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
processor.producer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
processor.consumer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
=====
package Demo10;
import java.util.Scanner;
public class Processor {
public void producer() throws InterruptedException {
synchronized (this) {
System.out.println("Producer thread running ......");
wait();
System.out.println("Resumed ......");
}
}
public void consumer() 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();
}
}
}
No comments:
Post a Comment