[英]IllegalMonitorStateException when creating an object
I'm getting IllegalMonitorStateException
when I'm trying to create an instance of an object. 尝试创建对象的实例时,出现
IllegalMonitorStateException
。 The code looks like the following: 该代码如下所示:
public int signIn(parameters...)
{
...check some stuff...
new Thread(... just a simple log here...).start();//IllegalMonitorStateException
return result;
}
MORE INFO: 更多信息:
The actual application consists of 2 programs (C++ and java) interacting via JNI). 实际的应用程序包含2个通过JNI进行交互的程序(C ++和Java)。 The scenario in which I'm getting exception is as follows.
我遇到异常的情况如下。
I should note that this exception only happens in this special scenario and if I call login sometime after connection success everything works fine. 我应该注意,此异常仅在这种特殊情况下发生,并且如果在连接成功后的某个时间调用login,则一切正常。
What I've tried: 我尝试过的
EDIT: 编辑:
Here's the stacktrace: 这是堆栈跟踪:
Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener.onConnectingFinished(Native Method) Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener.access$000(NativeAPIEventListener.java:12) Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener$1.run(NativeAPIEventListener.java:30) java.lang.Thread.run(Unknown Source)
Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener.onConnectingFinished(本机方法)Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener.access $ 000(NativeAPIEventListener.java:12)Phoenix.client.ClientAPI.NativeInterface.NativeAPIEventListener $ 1.run(NativeAPIEventListener.java :30)java.lang.Thread.run(未知来源)
I created a new thread in C++ code when java code called back into it This broke the jthread:java -> c++ -> java
chain into jthread:java -> c++
and cthread:c++ -> java
. 我在C ++代码中创建了一个新线程,当Java代码被调用回去时,这将
jthread:java -> c++ -> java
链jthread:java -> c++
到jthread:java -> c++
和cthread:c++ -> java
。 This solved the problem I was facing. 这解决了我面临的问题。 However I ran into a different problem which lead me into reading a bit of JNI documentation.
但是我遇到了一个不同的问题,这使我不得不阅读一些JNI文档。 Quoting JNI doc :
引用JNI doc :
The JNI interface pointer (JNIEnv) is valid only in the current thread.
JNI接口指针(JNIEnv)仅在当前线程中有效。 Should another thread need to access the Java VM, it must first call AttachCurrentThread() to attach itself to the VM and obtain a JNI interface pointer .
如果另一个线程需要访问Java VM,则必须首先调用AttachCurrentThread()将其自身附加到VM并获取JNI接口指针 。 Once attached to the VM, a native thread works just like an ordinary Java thread running inside a native method.
一旦附加到VM,本机线程就可以像在本机方法中运行的普通Java线程一样工作。 The native thread remains attached to the VM until it calls DetachCurrentThread() to detach itself.
本地线程将保持连接到VM,直到它调用DetachCurrentThread()使其脱离。
So I guess I should've called AttachCurrentThread
before calling back into java. 所以我想我应该在调用Java之前先调用
AttachCurrentThread
。 However this does not exactly fit in the above description since the thread was not a native thread (it was a thread originally created in java code, could I call DetachCurrentThread
afterwards?). 但是,由于该线程不是本机线程(它是最初在Java代码中创建的线程,此后可以调用
DetachCurrentThread
吗?),因此这与上面的描述并不完全匹配。 I did not test this solution since I had to create a new thread other reasons too. 我没有测试此解决方案,因为我也不得不基于其他原因创建新线程。 But if I get a chance to try this out I'll confirm.
但是,如果我有机会尝试一下,我会确认。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.