One problem is that take
is going to block on an empty queue (poll
is the method that returns null on an empty queue), so your loop won't terminate (unless parser
is adding a null to the queue, which ought to be an error). You're also calling take
too often - the first take
call (in the while loop's guard) is removing an element, and then the second take
call (inside the while loop) is removing another element, which means that you're discarding half of the queue's elements. Try something like this instead
try {
String temp;
while((temp = handler.bq.poll()) != null) {
// process temp
}
}
This will only work if you don't start takeOff
until parser
is finished adding items to the queue (when offer
returns false) - otherwise poll
might return null and terminate takeOff
while parser
is still adding items to the queue. Here's an alternative that lets you run parser
and takeOff
at the same time
String temp;
try {
while(true) {
temp = handler.bq.take();
// process temp
}
catch (InterruptedException ex) {
// ignore exception
}
while((temp = handler.bq.poll()) != null) {
// process temp
}
Then you need to interrupt()
the takeOff
thread when parser
is finished: this will exit the while(true)
loop, after which the thread will go to the while((temp = handler.bq.poll()) != null)
loop to finish removing items from the queue.