Hi, I have the following code that retrieves the first element off a queue only if it has at least 2 elements. For some reason, it's not polling the first element. However, if I add a print statement in there, it will print and poll. The method is in a thread, and there's another thread adding element to the queue, this thread reads from the queue.
...
public void run(){
while(beginning){
int size = queue.size();
// adding this will cause the program to enter if below: System.out.println(size);
if(size > 1){
System.out.println("data: " + queue.poll());
beginning = false;
}
}
}
...
If the println statement is added, it will print
1
1
....
2
data: data
If the println statement is remove, it will not print anything.
Actually, as long as I put something in there, a thread.sleep(1) or a random print statement, it will poll out the data and print it
Thanx for any input.