Reputation: 6958
I have a runnable created this way:
private Runnable _animationScriptRunnable = new Runnable() {
public void run() {
synchronized (AnimationManager.this) {
while (!_stopRunning && !_animationScriptStack.isEmpty()) {
Class key = _animationScriptStack.removeFirst();
if (isAnimationExist(key) && isAnimationActivated(key)) {
AAnimation animation = _animationsClassTable.get(key);
animation.doBeforeAnimation();
animation.onAnimationBeginning();
do {
animation.onAnimation();
} while (isAnimationActivated(key) && animation.isAnimationRecurent() && !_stopRunning);
animation.onAnimationEnding();
animation.doAfterAnimation();
}
}
}
}
};
As you can see, I check in a synchronized block that my stack (_animationScriptStack
, created as a LinkedList<Class<?>> _animationScriptStack
) is not empty, and if it's not, I remove the first element. But, sometimes, I have a java.util.NoSuchElementException
, on the removeFirst()
call.
Can someone explain me why?
Upvotes: 1
Views: 2932
Reputation: 13709
LinkedList
is not thread safe and probably this is causing un-expected results in your code.
You need to use ConcurrentLinkedQueue
in this case as you are trying to access a list in a Thread. Please look at it, I guess this is what you need.
Upvotes: 2