package lab2;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.An InputStreamReader is;
class Q {
int n;
boolean valueSet = false;
synchronized int get() {
while(!valueSet)
try {
wait () ;
} catch(InterruptedException e) {
System.out.println("InterruptedException intercepted");
}
System.out.println("Received:" + n) ;
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
while(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException intercepted");
}
this.n = n;
valueSet = true;
System.out.println("Sent:" + n); notify();
}
}
class Producer implements Runnable {
boolean pushButton;
Q Q;
Producer(Q Q) {
this.q = q;
new Thread(this, "Supplier").start() ;
}
public void run() {
int i = 0;
while(pushButton = true) {
q.put(i++);
}
BufferedReader br = new BufferedReader(new an InputStreamReader(System.in));
try
{
int ascii = br.read();
while (ascii != 10){
pushButton = false;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
boolean pushButton;
Q Q;
Consumer(Q Q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(pushButton = true) {
q. get();
}
BufferedReader br = new BufferedReader(new an InputStreamReader(System.in));
try
{
int ascii = br.read();
while (ascii != 10){
pushButton = false;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class Lab2 {
public static void main(String[] args)
{
Q Q = new Q ();
new Producer(q);
new Consumer(q);
}
}
Find more questions by tags Java