views:

51

answers:

1

What does it mean when a IllegalStateException is thrown when entering a synchronized block? I'm seeing this sometimes inside the run-method of a thread:

    public void run() {
        while (true) {
            int n = 0;
            synchronized (service) { // IllegalStateException
                n = processPendingRequests();
            }

            /*
             * If n > 0, we processed at least one element, in which case we
             * immediately check the queue again until it was empty.
             */
            if (n == 0) {
                sleep();
                continue;
            }
        }
    }

Can the service object cause the IllegalStateException? How?

+1  A: 

According to the Java Language Specification, the "synchronized" statement doesn't throw an "IllegalStateException". Therefore, either the Language Specification is wrong, you're using a non-conforming JVM, or you've misinterpreted the behavior of your program -- as far as I can see.

Steve Emmerson
I have probably misinterpreted something, yes.
JesperE