Question

Note: I'm not looking for workarounds; I'm sure I can find other methods if necessary. I simply feel like I'm missing something fundamental or quirky and I want to know what I'm missing. Or if there is a way to use the debugger to get more info that would be nice too. Thanks!

I'm having an issue with use of synchronized. I'm receiving deadlock but it seems utterly impossible. I've placed print statements before each and every synchronized call, just inside each call, and just before exiting so I can see who all holds which synchronized objects. I'm finding that it will not go inside one of my synchronized calls even though no one currently holds the lock on the object. Are there some kind of quirks that I'm missing or illegal nesting operations? Here's the jist of what I am doing.

Oh yeah, and the oddest thing is that removing the two "busyFlagObject" synchronizations makes it work fine...

Thread 1:

public void DrawFunction()
{
    synchronized(drawObject)
    {
        ...
        // Hangs here though nobody has a lock on this object
        synchronized(animationObject)
        {

        }
    }
}

Thread 2:

public void AnotherFunction()
{
    synchronized(busyFlagObject)
    {
        // Calls a function that also uses this same Synchronized call
        synchronized(busyFlagObject)
        {
            // Calls another function that uses another Synchronized call
            // Hangs here waiting for the draw function to complete which it SHOULD 
            // be able to do no problem.
            synchronized(drawObject)
            {

            }

            // Never gets to this one assuming the Log statements don't 
            // buffer and aren't flushed but still shouldn't be a problem anyway.
            synchronized(animationObject)
            {

            }
        }
    }
}
Was it helpful?

Solution

Run your app under the debugger or use "jstack" from the JDK tools. That will show you directly which threads wait for locks and which hold locks, so we don't have to guess where your problem is :-)

That said, you mention you synchronize on Boolean. Keep in mind that the class is intended to only have two instances, and many things (particularly boxing) will implicitly change your Boolean instance to the "shared" value. Are you sure your lock objects are not the same instance? You might consider using new Object() as your monitor object.

It's worth noting that this isn't the only place that this can happen and there's a good entry on this problem in Java Concurrency in Practice, specifically with string interning, that I'm failing to find a link to at the moment. Don't use a type that isn't under your control as something it wasn't intended to do :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top