尝试输入synchronized块时出现IllegalStateException

时间:2010-02-19 15:41:45

标签: java concurrency

进入同步块时抛出IllegalStateException是什么意思?我有时会在线程的run方法中看到这个:

    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;
            }
        }
    }

service对象可以导致IllegalStateException吗?怎么样?

1 个答案:

答案 0 :(得分:1)

根据Java Language Specification,“synchronized”语句不会抛出“IllegalStateException”。因此,无论语言规范是错误的,您使用的是不符合规范的JVM,或者您误解了程序的行为 - 据我所知。