I've ran into a strange situation, when I try to use a member variable in the thread I create, it doesn't return the correct value, below is a snippet of my code
new Thread() {
public void run() {
while (true) {
//System.out.println(printQueue.isEmpty());
while ((currentOrder = printQueue.poll())!= null) {
printall();
}
}
}
}.start();
printQueue
is a queue I defined, the result is different when I try to push a value into the queue.
when I uncomment the print line,this currentOrder
will return a non-null variable,but if I comment that line, currentOrder
will always return null.
How does it happen and what am I supposed to do to resolve it?