进入同步块时抛出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
吗?怎么样?
答案 0 :(得分:1)
根据Java Language Specification,“synchronized”语句不会抛出“IllegalStateException”。因此,无论语言规范是错误的,您使用的是不符合规范的JVM,或者您误解了程序的行为 - 据我所知。