如果我对 Thread.interrupt() 的调用不起作用,如何正确停止线程?

发布于 2024-11-03 08:04:14 字数 299 浏览 1 评论 0原文

众所周知,不应使用 Thread.stop() 停止正在运行的进程。

通常,手册和教程建议使用 Thread.interrupt() 或某些布尔变量,并从代码内部检查该中断或变量。

但是,如果我有一个库方法,有时需要很长时间才能执行,并且我想让用户能够停止该过程?并且库没有给我一个机制来做到这一点(不检查线程中断状态,并且没有“停止!”变量)?

而且,更糟糕的是,要么没有库的源代码,要么它太大而无法对其进行编辑并在适当的位置添加检查。

看来 Thread.stop() 是这里唯一的解决方案。或者也许有一些解决方法?

It is a widely known fact that one shall not stop running processes using Thread.stop().

Usually the manuals and tutorials suggest using Thread.interrupt() or some boolean variable instead, and checking from inside the code for that interrupt or variable.

But if I have a library method which takes a very long time to execute sometimes, and I want to give user an ability to stop that process? And library does not give me a mechanisms to do it (does not check thread interrupted status, and no "stop!" variables)?

And, to add to the bad things, there is either no source code for library, or it is just too big to edit it and add checks at appropriate places.

It seems that Thread.stop() is the only solution here. Or maybe there is some workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗酒趁年少 2024-11-10 08:04:15

为什么在等待某些条件时不尝试使用睡眠(长时间超时),如果没有成功,则简单地从线程“返回”?


也许你的线程正在运行在 while (booleanVariable) { } 中,

如果是这样,你可以将此变量设置为 volatile,并且线程控制器将其设置为 false。

Thread.stop() 想象为 System.exit(value),它可以工作,但是当您遇到一些错误导致线程停止/vm 退出时,将会更难找到它。

Why you do not try to use sleep(long timeout) when are waiting for some condition and if had not success, you simple "return" from the thread?


Probably your thread is running in a while (booleanVariable) { },

if it is, you could set this variable as volatile, and the thread controller set it as false.

Think of the Thread.stop() like the System.exit(value), it works, but when you have some bug making you thread stop/vm exit, will be much more harder to find it out.

傲世九天 2024-11-10 08:04:15

如果您的情况可行,请生成另一个进程并将其视为工作单元,而不是线程。进程终止更具确定性,尽管将一个进程专门用于以前线程的工作对于您的情况来说可能过于重量级。

If practical in your situation, spawn another process and treat that as the unit of work, rather than a thread. Process killing is much more deterministic, though devoting a process to what used to be a thread's work might be too heavyweight for your situation.

尹雨沫 2024-11-10 08:04:15

比使用 Thread.stop() 更好的唯一解决方案是在单独的线程中使用该库,您可以杀死该线程来停止它。

The only solution better than using Thread.stop() is to use the library in a seperate thread which you can kill to stop it.

厌倦 2024-11-10 08:04:15

您可能想要查找正在运行的函数的不同句柄,例如,如果是 IO,您可以尝试关闭任何打开的连接/流。如果您被这个库困住了(IE 找不到具有更好中断机制的库),Thread.stop() 是停止线程的唯一方法。

You may want to look for different handles of the function you are running, for example if its IO you can try to close any open connections/streams. If you are stuck with this library (IE can't find one that has better interruption mechanics) Thread.stop() is your only way of stopping the thread.

迷鸟归林 2024-11-10 08:04:15

从 java 4 开始,Thread.stop() 已被弃用。我读了一篇文章,通过将对库的调用包装在一个实现 InterruptibleChannel(属于 java.nio 的一部分)的单独类中来停止线程。
Interruptible类有close()方法,通过该方法另一个线程可以异步调用它。

Thread.stop() is deprecated from java 4 onwards..I read an article to stop a thread by wrapping the call to the library in an separate class that implements InterruptibleChannel which is part of java.nio.
Interruptibleclasses has close() method, through which another thread can call it asynchronously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文