I am implementing a producer and consumer pattern using ArrayDeque and running into a strange problem.
Consumer.java
Class Consumer {
public final Queue<Msg> my_queue = new ArrayDeque<Msg>();
public void begin() {
new Thread() {
new Runnable() {
public void run() {
while(true) {
while(my_queue.isEmpty()) {
// do nothing
}
Msg msg = my_queue.remove();
msg.doSomething();
}
}
}
}.start();
}
}
Since my_queue is public, I may have > 1 threads performing my_queue.add(msg). However, the strangeness occurs because doSomething is never called when running in production. However, when i am in debug mode and use a break point, doSomething() will be called! Can anyone explain this?