I'm trying to figure out a way to implement the following in Java.
Thread1 will add jobs to queue1. Another different thread (Thread2) will add jobs to queue2.
In the run() method of Thread1 I wait until there's a job in queue 1, and let's say I will print it, if and only if there are no awaiting jobs in queue2.
How may I notify Thread1 that Thread2 has added a job in queue2? Here is Thread1 Class
public class Thread1 implements Runnable {
private List queue1 = new LinkedList();
public void processData(byte [] data, int count) {
byte[] dataCopy = new byte[count];
System.arraycopy(data, 0, dataCopy, 0, count);
synchronized(queue1) {
queue1.add(data);
queue1.notify();
}
}
public void run() {
byte [] data;
while(true) {
// Wait for data to become available
synchronized(queue1) {
while(queue1.isEmpty()) {
try {
queue1.wait();
} catch (InterruptedException e) {}
}
data = (byte[]) queue1.remove(0);
}
// print data only if queue2 has no awaiting jobs in it
}
}