简体   繁体   English

Pthread,杀死由JNI创建的线程

[英]Pthread, kill the thread has created by JNI

In our project, some of the codes are native, and invoked by JNI, so everything is looking good unless we decided to run some invokes parallel, but the issue(the unclear thing) is that either run the thread by Java or pthread?! 在我们的项目中,一些代码是本地代码,并由JNI调用,因此,除非我们决定并行运行一些调用,否则一切看起来都很好,但是问题(不清楚的是)是通过Java还是通过pthread运行线程?

We prefer pthread because of its better management and power, but the question is. 我们更喜欢pthread,因为它具有更好的管理和功能,但问题是。

suppose a native code starts a pthread, so we need to have the pthread reference in order to cancel/exit the thread with another method, so the first question is, how would I retunr the pthread id to the java code, is it possible, is pthread id is int? 假设本机代码启动了一个pthread,所以我们需要pthread引用才能用另一种方法取消/退出该线程,所以第一个问题是,如何将pthread id调整为Java代码,是否可能,是pthread id是int吗?

native int start_thread();

the next question is, is it possible to pass a Java object(interface) to pthread instead of a native method? 下一个问题是,是否可以将Java对象(接口)传递给pthread而不是本机方法? something like this 像这样的东西

native int start_thread(pthread_start ref);//where pthread_start is a java interface with one method, like Runnable

and the last question, once we got the pthread reference, is it possible to just pass it to another native method to exit/cancel it? 最后一个问题,一旦获得了pthread引用,是否可以将其传递给另一个本机方法以退出/取消它?

native void kill_thread(int pthread_id);

You can store the pointer to your pthread_t structure within Java, so long as you 'new' it in your native code. 您可以在Java中存储指向pthread_t结构的指针,只要在本机代码中“新建”它即可。

With that pointer, you can call back into your native code for thread management(join, kill). 使用该指针,您可以调用本机代码进行线程管理(join,kill)。

Likewise, when you are starting your thread, you can pass a reference to your Java object, which will arrive in the native code as a pointer. 同样,在启动线程时,可以将引用传递给Java对象,该引用将作为指针到达本机代码中。 While I don't recommend trying to call your Java function(ie Runnable ) directly from your pthread_create call, you can pass that Java object pointer to your native thread routine, which in turn can invoke your Java method via normal JNI calls. 虽然我不建议您尝试直接从pthread_create调用中调用Java函数(即Runnable),但是您可以将该Java对象指针传递给您的本机线程例程,该例程进而可以通过普通的JNI调用来调用Java方法。

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

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