java结束函数,Java:如何使用JNI函数停止调用一系列C ++函数的Thread?

So, here is my situation :

I've got a Java application that is sending arrays of data to a C++ DLL using a JNI method from a "JavaToCpp" class.

Once the C++ DLL have received all the data, it begins performing several actions on it.

I'm running the "JavaToCpp" class using a new thread, for my Java interface not to be frozen during the (long) (C++) procedures/subroutines.

I implemented two methods to stop the working (C++) procedures/subroutines :

The first one "STOP" : is creating a file which is going to be read by the C++ DLL so it can cleanly stop the running procedures/subroutines.

The second one "KILL" : is supposed to shutdown / kill the running C++ procedures/subroutines directly

The problem is that, after looking for a while, I did not find any good trick to perfom it.

So if anyone's got an idea on how to kill a Thread while procedures/subroutines are being running ...

解决方案

The only way in to "kill" a Thread in Java is the deprecated stop() method.

Before you plan to use it, read and understand why it's deprecated. If you understand it, you won't use it.

So no, there's no good way to kill a thread. But why are you talking about C++ processes (or did you mean procedures, i.e. subroutines)? Because if you use a separate process (not thread) to do the native part, the you could easily stop it: use ProcessBuilder and Process.destroy(). There are other advantages too in using processes instead of threads to invoke native stuff. One benefit is that an unstable native process won't kill your Java process.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值