简体   繁体   English

创建对象时发生IllegalMonitorStateException

[英]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. 我遇到异常的情况如下。

  • The c++ program asks java to connect to a server. C ++程序要求Java连接到服务器。 (this is a non blocking operation) (这是一个非阻塞操作)
  • Java program informs c++ about connection success. Java程序通知c ++连接成功。 (in a new thread so that java can continue doing other tasks) (在新线程中,以便java可以继续执行其他任务)
  • When receiving connection success, c++ program asks java to login 收到连接成功后,C ++程序要求Java登录
  • Exception occurs. 发生异常。

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: 我尝试过的

  • In the beginning informing connection success was not in a new thread, but creating the thread did not solve the problem. 最初,通知连接成功不是在新线程中,但是创建线程并不能解决问题。
  • The java login code had some synchronization stuff but removing them and replacing it with a simple log still produces the problem. Java登录代码具有一些同步功能,但是删除它们并用简单的日志替换仍然会产生问题。

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++ -> javajthread: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.

相关问题 中断线程时发生IllegalMonitorStateException - IllegalMonitorStateException when interrupting threads 通知线程时发生IllegalMonitorStateException - IllegalMonitorStateException when notifying threads 抛出:IllegalMonitorStateException - IllegalMonitorStateException 当我添加 <mvc:annotation-driven /> ,出现“创建bean时出错”,“ java.lang.IllegalMonitorStateException” - When I add <mvc:annotation-driven />, I get a “Error creating bean”, “java.lang.IllegalMonitorStateException” 在Integer上同步时notify()上发生IllegalMonitorStateException - IllegalMonitorStateException on notify() when synchronized on an Integer IllegalMonitorStateException:对象在notify()之前没有被线程锁定 - IllegalMonitorStateException: object not locked by thread before notify() 如何在使用wait和notify时修复IllegalMonitorStateException? - How to fix IllegalMonitorStateException when using wait and notify? 在 Android 应用程序中使用锁和条件时出现 IllegalMonitorStateException - IllegalMonitorStateException when using locks and conditions in Android app 在Reentrantlock上使用lockInterruptibly时如何避免IllegalMonitorStateException - How to avoid IllegalMonitorStateException when using lockInterruptibly on Reentrantlock 使用PowerMock避免wait()方法时发生IllegalMonitorStateException - IllegalMonitorStateException when using PowerMock to avoid wait() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM