java jni 文件 中断操作,停止调用JNI函数的Java线程

Here I want to stop my thread or kill my thread which is created on Java layer, and this thread is calling JNI function. Sometimes as per my application requirement, I have to stop this JNI function execution on some conditions if its going on, otherwise not.

new Thread(new Runnable() {

@Override

public void run() {

// My jni function call, It calls my JNI layer C function.

}

}

Now when this thread execution is started and its doing work at JNI level I have no worries about that, but from other class or methods on some condition I want to stop this JNI work so how can I stop this thread.

Note: Here my thread also has no while-loop so I cant check with some global flag variable also.

So does anyone have an idea on how to kill a thread while its call any JNI function without while loop.

解决方案

You can't safely interrupt a thread, if it is executing a native code. Even if your thread had an event loop, you need to wait until it finishes the native call. Knowing nothing about your code, i would guess that you had a long running native call, you didn't want it to clog the main thread, so you created a separate thread for that call. There is no way you can safely interrupt a single native call. No silver bullet here. You need to change your code in any case, my suggestions would be:

decompose your single long native call into series of short calls and run event loop on Java side

decompose the native call internally on the native side and run the event loop on native side. Your native interface will need another method for setting the interruption flag.

Thread.interrupt() won't help you because "calling native function" doesn't fall under any of the interruptible actions specified in Javadoc. The Java thread will keep running, only it's interrupt status will be set.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值