For some reason, the output of this:
public void msgNeedParts() {
// Blabla...
System.out.println(name + ": Try to print 'tasks'...");
synchronized(tasks) {
System.out.println(name + ": Tasks--" + tasks);
System.out.println(name + ": Did I manage to print it?");
tasks.add(new BinToDump(feeder, binNum));
}
stateChanged();
}
Just prints out “GantryAgent: Try to print ‘tasks’…” but not any of the following messages. I’m guessing the thread somehow ‘gets stuck’ when trying to access the synchronized list ‘tasks’, but I don’t know why this is happening.
‘tasks’ was declared and initialized like this:
private List<BinToDump> tasks =
Collections.synchronizedList(new ArrayList<BinToDump>());
Can anybody point out what I’m missing?
Ah! I suspect I may have a culprit:
/* If nothing left to do, return to original position. */
synchronized (tasks) {
if (tasks.isEmpty()) {
doReturnToOriginalPos();
}
}
In my scheduler (this is an agent design), I check to see if ‘tasks’ is empty, then I call doReturnToOriginalPos(). Maybe this is just happening over and over so fast that other methods don’t get a chance to modify it?
That was indeed the problem! It kept getting called so fast in my scheduler that nothing else could access ‘tasks’. Thanks all for the help!
Something has a lock on tasks. Depending on what sort of application this is, you should be able to get a full stack dump of the system, but the method varies. For example, I think CTRL-Break on most windows-based appservers will do this, and I think sending a SIGQUIT on linux will do the same.
Once you get a stack dump, you can look through it to try and find out which other thread has a lock on that object.
You can also use VisualVM to get a stack dump, for the same end goal: