This is a follow-up question from my previous question asked here.
I am using a PriorityBlockingQueue
now. I changed my producer to the following:
synchronized(Manager.queue) {
Manager.queue.add(new Job());
Manager.queue.notify();
}
And changed Consumer
to the following. Full code skeleton is here:
//my consumer thread run()
public void run() {
synchronized(Manager.queue) {
while (Manager.queue.peek() == null) {
System.out.println("111111111111111");
try {
Manager.queue.wait();
} catch (InterruptedException e) {
}
}
Job job=Manager.queue.peek();
if (job != null) {
submitJob(job);
if (job.SubmissionFailed.equals("false")) {
// successful submission. Remove from queue. Add to another.
Manager.queue.poll();
Manager.submissionQueue.put(job.uniqueid, job);
}
}
}
My code only works for the first time (first produce and first consume), but it doesn't work for the second time. Somewhere the wait/notify logic fails I guess. The producer pushes new jobs to the queue, but the consumer doesn't peek
any more items. In fact, it doesn't even go to the while
loop and no more 111111111111111
printing.
What is the problem? How to fix it?