Java:如何使用JNI函数停止调用一系列C ++函数的Thread? [英] Java : How to stop a Thread that's calling a succession of C++ functions using a JNI function?

查看:392
本文介绍了Java:如何使用JNI函数停止调用一系列C ++函数的Thread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的情况:

我有一个Java应用程序,它使用来自JavaToCpp的JNI方法将数据数组发送到C ++ DLL class。

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

一旦C ++ DLL收到了所有数据,它就开始对它执行几个操作。

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

我正在使用新线程运行JavaToCpp类,因为我的Java接口在(长)(C ++)过程/子例程期间不会被冻结。

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

我实现了两种方法来停止工作(C ++)程序/子程序:

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


  • 第一个停止:正在创建一个文件将由C ++ DLL读取,以便它可以干净地停止正在运行的过程/子例程。

  • 第二个KILL:应该直接关闭/终止正在运行的C ++过程/子例程

问题是,在找了一段时间后,我没有找到任何好的技巧来实现它。

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

所以,如果有人知道如何ki在程序/子程序正在运行时运行一个线程...

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

推荐答案

用Java杀死一个线程的唯一方法是已弃用 stop()方法。

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.

所以不,没有好方法可以杀死一个线程。但是你为什么要谈论C ++ 进程(或者你的意思是程序,即子程序)?因为如果使用单独的进程(而不是线程)来执行本机部分,则可以轻松地将其停止:使用 ProcessBuilder Process.destroy()。使用进程而不是线程来调用本机的东西也有其他优点。一个好处是不稳定的本机进程不会杀死你的Java进程。

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.

这篇关于Java:如何使用JNI函数停止调用一系列C ++函数的Thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆