简体   繁体   English

从C(JNI)DLL调用时,Java应用程序不会启动线程

[英]Java app does not start a thread when called from C (JNI) DLL

I've created a java library in which a thread is started. 我创建了一个其中启动线程的Java库。

When testing this library with a normal java-main method and calling the library, the thread in question just starts fine. 当使用普通的java-main方法测试该库并调用该库时,所讨论的线程就可以正常启动。

But now I want to call this library from a C dll via JNI. 但是现在我想通过JNI从C dll调用此库。

1) So, the C dll calls a library method foo via JNI 1)因此,C dll通过JNI调用库方法foo

2) foo creates a java thread and calls start on it 2) foo创建一个Java线程,并呼吁start就可以了

3) foo returns to the C dll caller. 3) foo返回到C dll调用程序。

4) Java thread now starts 4)Java线程现在开始

The problem is that I get a Future back from the thread and I call get on it. 问题是,我得到一个Future从螺纹回来,我呼吁get它。

This get blocks forever when the java code is called from the C dll, but works fine when called from another java app. 从C dll调用Java代码时,此操作get永远阻塞,但从另一个Java应用程序调用时,此方法会正常工作。

All other code is running also fine with JNI. 使用JNI,所有其他代码也可以正常运行。

Any ideas how this can happen? 任何想法如何发生?

Is the dll or JNI somehow preventing java thread creation? dll或JNI是否以某种方式阻止了Java线程的创建?

In case someone faced a similar problem: 如果有人遇到类似的问题:

The issue was that I created a native callback to the dll. 问题是我创建了对该dll的本地回调。

This callback is called from the created thread and multithreading on the native code was not allowed. 从创建的线程中调用此回调,并且不允许在本机代码上进行多线程处理。

Removing all calls from the thread to native code solved the problem. 删除线程对本机代码的所有调用即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM