java并发线程_java并发如何杀死线程

java并发线程

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume(), and stop() were used to manage the execution of threads. But these methods were deprecated by Java 2 because they could result in system failures. Modern ways to suspend/stop a thread are by using a boolean flag and Thread.interrupt() method.

run()方法完成后,线程将自动销毁。 但是可能需要在线程结束其生命周期之前将其杀死/停止。 以前,方法suspend()resume()stop()用于管理线程的执行。 但是Java 2不赞成使用这些方法,因为它们可能导致系统故障。 暂停/停止线程的现代方法是使用布尔标志和Thread.interrupt()方法。

线。 中断 () (Thread.interrupt())

If any thread is in sleeping or waiting for the state then using interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread that is in the sleeping or waiting state can be interrupted with the help of interrupt() method of Thread class.

如果有任何线程处于睡眠或等待状态,则使用interrupt()方法,我们可以通过显示InterruptedException来中断该线程的执行。 可以通过Thread classinterrupt()方法来中断处于睡眠或等待状态的线程

Whenever an interrupt has been sent to a thread, it should stop whatever task it is performing. It is very likely that whenever the thread receives an interrupt, it is to be terminated.

每当将中断发送到线程时,它都应停止正在执行的任何任务。 每当线程接收到中断时,很有可能将其终止。

This action can be done by using the interrupt() method. Whenever Thread.interrupt() is called, it sets a flag known as the interrupt status to true. This means that the thread has to stop performing further execution. The default value of this flag is false.

可以通过使用interrupt()方法来完成此操作。 每当调用Thread.interrupt() ,它都会将称为中断状态的标志设置为true。 这意味着线程必须停止执行进一步的执行。 此标志的默认值为false。

中断状态标志 (The Interrupt Status Flag)

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt() sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

中断机制是使用内部标志(称为中断状态)实现的 。 调用Thread.interrupt()设置此标志。 当线程通过调用静态方法 Thread.interrupted检查中断时,将清除中断状态。 一个线程用于查询另一线程的中断状态的非静态isInterrupted方法不会更改中断状态标志。

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

按照约定,任何通过抛出InterruptedException退出的方法都会清除中断状态。 但是,总是有可能另一个线程调用interrupt再次立即设置中断状态。

支持中断 (Supporting Interruption)

How does a thread support its own interruption? This depends on what it’s currently doing. If the thread is frequently invoking methods that throw InterruptedException, it simply returns from the run method after it catches that exception. For example:

线程如何支持自己的中断? 这取决于它当前正在做什么。 如果线程经常调用引发InterruptedException的方法,则在捕获该异常后,它仅从run方法返回。 例如:

The Output will be:

输出将是:

Main thread execution completed.
Starting MyThread execution.
InterruptedException occur

Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.

许多引发InterruptedException方法(例如sleep )被设计为取消其当前操作并在收到中断时立即返回。

What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:

如果线程长时间运行而没有调用引发InterruptedException的方法怎么办? 然后,它必须定期调用Thread.interrupted ,如果已接收到中断,则返回true 。 例如:

The Output will be:

输出将是:

MyThread is running.
MyThread is running....MyThread is running.
MyThread has been stopped.
Exiting the main Thread.

In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an InterruptedException:

在这个简单的示例中, 代码仅测试了中断,如果已接收到该线程,则退出线程。 在更复杂的应用程序中,抛出InterruptedException可能更有意义:

if (Thread.interrupted()) {
throw new InterruptedException();
}

This allows interrupting handling code to be centralized in a catch clause, like with a Thread.sleep() method.

这允许将中断处理代码集中在catch子句中,例如使用Thread.sleep()方法。

结论 (Conclusion)

In this article, we learned how to kill a thread, what is interruption status, and how to create an interruptible custom thread.

在本文中,我们学习了如何杀死线程 ,什么是中断状态以及如何创建可中断的自定义线程。

In the next article, we will check what is Thread Safety. Stay tuned.

在下一篇文章中,我们将检查什么是线程安全 。 敬请关注。

Found this article useful? Follow me (Dmytro Timchenko) on Medium and check out my other articles below! Please 👏 this article to share it!

觉得这篇文章有用吗? 在Medium上关注我( Dmytro Timchenko ),并在下面查看我的其他文章! 请👏这篇文章分享!

翻译自: https://medium.com/javarevisited/java-concurrency-how-to-kill-a-thread-a170d46158e4

java并发线程

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值