NoSuchElementException on Queue

Wednesday, July 2, 2014

I'm having some problems with a queue in Java. It's declared like:



Queue<MotionEvent> touchQueue = new LinkedList<MotionEvent>();


I add elements only in one point (but on a callback, most likely called from a thread):



@Override public boolean onTouchEvent(MotionEvent me)
{
touchQueue.add(me);
return true;
}


And I consume the events on the main thread with a call to a syncronized function:



public synchronized void UpdateTouches()
{
while(!touchQueue.isEmpty())
{
try {
MotionEvent me = touchQueue.poll();
ProcessTouchEvent(me);
} catch (NoSuchElementException e)
{
return;
}

}
}


The problem is that sometimes poll is rising a NoSuchElementException and after that all subsequent poll calls will raise that exception. Anyone has any idea what might be the cause ? Or, is there any way to remove the head object without getting the exception again? Please note that size() returns > 0 when the exception occurs,


Thanks, /C.







http://ift.tt/1o7iPeT