Can you make a small, simple, single program file that shows the problem you are working on? Or merge the six files into one file and post that.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
How do you test the posted code? I don't see a main() method?
Can you post the full text of the error message? I can not find: IllegalMonitorException
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
In Producer class inside messageConsume() method, did a call from consumer go through the loop down to the checking for region? If so, did it actually get through inside the checking region if-condition? Try to print out each line of your process to pin point where exactly is wrong.
Anyway, I see you use wait() method, but who is going to wake the thread up after the wait()? Where are you putting your notify()? A thread can't simply wake up after being wait(). The same goes to pushing message onto the queue. Once the queue is full, who is going to wake the thread up to keep pushing again?
public static Message messageConsume(String region) {
synchronized(sharedQueue) {
while (sharedQueue.isEmpty()) {
try {
sharedQueue.wait(); // Who is going to notify this thread???
} catch (InterruptedException e) { }
}
System.out.println("Queue size: "+sharedQueue.size());
if (sharedQueue.get(0).getRegion().equalsIgnoreCase(region)) {
Message tempMessage = sharedQueue.remove(0);
System.out.println("Removed: "+tmpMessage);
//sharedQueue.remove(0); // use remove() right away, don't need this
System.out.println("Queue size after removed: "+sharedQueue.size());
return tempMessage;
} else {
return null;
}
}
}
Personally, I would keep the synchronized block as small as I could. Also, I would keep an infinite loop out of the block (the current while loop). I would instead check if an item is returned after each loop; otherwise, I will wait.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
Question Answered as of 6 Months Ago by
NormR1
and
Taywin