Java 非法监视器状态异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1850422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalMonitorStateException
提问by BeefCake__beefcake
When running our program we get an exception of type java.lang.IllegalMonitorStateException. On Java6 API website, it says there is a constructor that gives a details about the exception: IllegalMonitorStateException(String s)
当运行我们的程序时,我们得到一个 java.lang.IllegalMonitorStateException 类型的异常。在 Java6 API 网站上,它说有一个构造函数可以提供有关异常的详细信息: IllegalMonitorStateException(String s)
How can we use this to get a better idea of where the bug is in our code? Is there anything else we can do (besides lots of debugging which we're currently doing) to pinpoint the function or line that failed?
我们如何使用它来更好地了解代码中的错误位置?我们还有什么可以做的(除了我们目前正在进行的大量调试)来查明失败的函数或行吗?
回答by Lawrence Dol
You should print the stack trace, which will give you the exact location in the source.
您应该打印堆栈跟踪,这将为您提供源中的确切位置。
Unfortunately it's not uncommon for the JVM to throw exceptions which contain no detail message to assist in debugging.
不幸的是,JVM 抛出不包含详细消息以帮助调试的异常并不少见。
回答by user85421
The details must be given when the Exception is created (Constructor, right?) and if you are not creating it, there is no way for you to provide the details.
创建异常时必须提供详细信息(构造函数,对吗?),如果您不创建它,则无法提供详细信息。
You can analize the StackTrace of the Exception. It shows the classes, methods and souce line which were called to cause the Exception.
您可以分析异常的 StackTrace。它显示了被调用导致异常的类、方法和源代码行。
One cause for the IllegalMonitorStateException
is trying to wait on an Object without having synchronized on it. See the Javadoc.
一个原因IllegalMonitorStateException
是试图等待一个没有同步的对象。请参阅Javadoc。
There are other possible causes and the Exception may be thrown by some library/external code. I think only the StackTrace can help...
还有其他可能的原因,某些库/外部代码可能会抛出异常。我认为只有 StackTrace 可以帮助...
回答by Stephen C
How can we use this to get a better idea of where the bug is in our code? Is there anything else we can do (besides lots of debugging which we're currently doing) to pinpoint the function or line that failed?
我们如何使用它来更好地了解代码中的错误位置?我们还有什么可以做的(除了我们目前正在进行的大量调试)来查明失败的函数或行吗?
In this case, printing the message by itself probably won't help much. What you need is a stacktrace with source file names and line numbers.
在这种情况下,单独打印消息可能不会有太大帮助。您需要的是带有源文件名和行号的堆栈跟踪。
Make sure that all relevant ".class" files / JARs were built with file and line number debug information included. This is the default, but compiling with "-g:none" will strip this ... as will most JAR file obfuscators.
Next, add a try / catch block to catch the
IllegalMonitorStateException
and either callex.printStackTrace()
or log the exception.
确保所有相关的“.class”文件/JAR 都包含文件和行号调试信息。这是默认设置,但使用 "-g:none" 编译将删除它......就像大多数 JAR 文件混淆器一样。
接下来,添加一个 try / catch 块来捕获
IllegalMonitorStateException
并调用ex.printStackTrace()
或记录异常。
From the stacktrace you should be able to see what line in the code threw the exception. The chances are that is was a call to Object.wait(...)
or something like that. Check the javadoc for the offending method to find out what circumstances cause the exception to be thrown.
从堆栈跟踪中,您应该能够看到代码中的哪一行引发了异常。很有可能是一个电话Object.wait(...)
或类似的东西。检查 javadoc 中的违规方法以找出导致抛出异常的情况。
(And once you are done, remember to move the try / catch block you added.)
(完成后,请记住移动您添加的 try / catch 块。)
回答by kmcguire
This is maybe occurring because the instance of the object which you are calling wait or notify on is different that the instance you synchronized with. For example:
这可能是因为您调用 wait 或 notify 的对象的实例与您同步的实例不同。例如:
Integer a;
a = new Integer(0);
synchronized(a) {
System.out.printf("I synchronized on %h.", a);
++a;
System.out.printf("But, I am calling notify for %h and I hold no lock for it.", a);
a.notify();
}
This will throw the IllegalMonitorStateException
because the instance that 'a' points to is no longer the same.
这将抛出 ,IllegalMonitorStateException
因为 'a' 指向的实例不再相同。