...
Code Block | ||
---|---|---|
| ||
public class MissedSignal implements Runnable {
private static final Object lock = new Object();
private static int buffer_count = 5;
private static int number; // Selects function based on thread number
public void setThreadNumber(int num) {
number = num;
}
public void run (){
synchronized(lock) {
try {
if(number == 1) {
while(buffer_count == 0) {
lock.wait();
}
} else if(number == 2) {
while(buffer_count == 10) {
lock.wait();
}
} else if(number == 3) {
lock.notify();
}
} catch (InterruptedException ie) {
// Handle the exception
}
}
}
public static void makeThread1True() {
buffer_count = 0;
}
public static void makeThread2True() {
buffer_count = 10;
}
public static void main(String[] args) throws IOException, InterruptedException {
MissedSignal ms = new MissedSignal();
makeThread1True();
ms.setThreadNumber(1);
new Thread(ms).start();
Thread.sleep(1000);
makeThread2True();
ms.setThreadNumber(2);
new Thread(ms).start();
Thread.sleep(1000);
ms.setThreadNumber(3);
new Thread(ms).start();
}
}
|
...