在 Java 8 中替代 Thread.stop()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23317332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:50:40  来源:igfitidea点击:

Alternate to Thread.stop() in Java 8?

javamultithreadingjava-8

提问by Luna

Is there any way to force a Threadto stop under Java 8, now that Thread.stophas been removed? This includes forcing a thread waiting on a lock/synchronized/sleep/wait/infiniteloop to stop.

有没有办法强制 aThreadJava 8下停止,现在Thread.stop已经被删除了?这包括强制等待锁定/同步/睡眠/等待/无限循环的线程停止。

This is intended as part of a watchdogfor the application which can detect if it has deadlocked/frozen, force kill threads and then save the current state to avoid lost information - while there is a high chance of this information being corrupt due to forcing a thread to stop, it's better to have a copy of the possibly incorrect information than to lose it.

这旨在作为应用程序看门狗的一部分,它可以检测它是否已死锁/冻结,强制终止线程,然后保存当前状态以避免丢失信息 - 虽然由于强制执行,这些信息很有可能被破坏线程停止,最好有一份可能不正确的信息的副本,而不是丢失它。

Attempting to save this information without stopping the locked up threads is impossible, as saving it will require acquiring locks.

在不停止锁定的线程的情况下尝试保存此信息是不可能的,因为保存它需要获取锁。

采纳答案by Holger

If you really need to stop a Threadthe hard way you may consider that the method Thread.stop()(without providing an arbitrary Throwable) still works with Java?8. It will generate a ThreadDeathon the stopped thread which can be handled like any other Error, despite its unusual name. The only difference is that no stack trace will be printed if ThreadDeathis not caught.

如果您真的需要停止一个Thread艰难的方式,您可能会认为该方法Thread.stop()(不提供任意Throwable)仍然适用于 Java?8。它将ThreadDeath在停止的线程上生成Error,尽管它的名称不寻常,但可以像其他任何 一样处理。唯一的区别是如果ThreadDeath没有被捕获,将不会打印堆栈跟踪。



But beware that this method is deprecated for most of the same reasons than the other stopmethod. It might become unspported in one of the next releases of Java. So it's still time to think about a plan B…

但请注意,由于大多数与其他stop方法相同的原因,此方法已被弃用。它可能在 Java 的下一版本中不受支持。所以现在是考虑B计划的时候了……

回答by peter.petrov

The right way to stop a thread is to call interrupton it.

停止线程的正确方法是调用interrupt它。

回答by Honza Zidek

Read these articles, they explain it:

阅读这些文章,他们解释了:

  1. "Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?" http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

  2. Joshua Bloch: "Effective Java (2nd Edition)", Item 66: Synchronize access to shared mutable data

  1. “为什么不推荐使用 Thread.stop、Thread.suspend、Thread.resume 和 Runtime.runFinalizersOnExit?” http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

  2. Joshua Bloch:“Effective Java(第 2 版)”,第 66 条:同步访问共享可变数据

Joshua Bloch explains how to stop a thread and says this:

Joshua Bloch 解释了如何停止线程并说:

Consider the task of stopping one thread from another. The libraries provide the Thread.stop method, but this method was deprecated long ago because it is inherently unsafe—its use can result in data corruption. Do not use Thread.stop. A recommended way to stop one thread from another is to have the first thread poll a boolean field that is initially false but can be set to true by the second thread to indicate that the first thread is to stop itself.

考虑从另一个线程停止一个线程的任务。库提供了 Thread.stop 方法,但该方法很久以前就被弃用了,因为它本质上是不安全的——它的使用会导致数据损坏。不要使用 Thread.stop。从另一个线程停止一个线程的推荐方法是让第一个线程轮询一个布尔字段,该字段最初为 false,但可以由第二个线程设置为 true 以指示第一个线程将停止自身。

When using this technique you should handle also synchronization of the boolean field. I don't want to copy too much stuff from Joshua Bloch's book, but you will find everything there and in the official Oracle Java documentation mentioned above.

使用此技术时,您还应该处理布尔字段的同步。我不想从 Joshua Bloch 的书中复制太多内容,但是您可以在那里和上面提到的官方 Oracle Java 文档中找到所有内容。