简体   繁体   English

尝试输入同步块时发生IllegalStateException

[英]IllegalStateException while attempting to enter synchronized block

What does it mean when a IllegalStateException is thrown when entering a synchronized block? 输入同步块时抛出IllegalStateException表示什么? 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 ? service对象可以引起IllegalStateException吗? How? 怎么样?

According to the Java Language Specification , the "synchronized" statement doesn't throw an "IllegalStateException". 根据Java语言规范 ,“同步”语句不会引发“ 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. 因此,就我所知,语言规范是错误的,您使用的是不合格的JVM,还是您误解了程序的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM