Java中的线程中断 [英] Thread Interrupt in java

查看:84
本文介绍了Java中的线程中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,如果我在运行15分钟的Java进程中中断了线程,它将停止该进程,停止该方法或将完成该进程,并且不允许其他人继续运行吗?

I have the following questions, if I interrupt a thread in java, a process that is running 15 minutes, it will stop the process, stop the methods or will finish this process,and not allow others continue in line?

我有一个线程",它将在插入postgres后调用一个基于访问"和"postgres"进行比较的方法,如果我想停止调用此操作的线程,她将成功执行停止该过程,还是等待它们完成?

I have a "thread" that will call a method that will do a comparison based "access" and "postgres" after it will insert in postgres, if I want to stop the thread that called this action, she will succeed the stop the process, or wait they finish?

推荐答案

除非在线程中执行的方法在接收到中断"信号后愿意终止,否则它不会停止线程.引发InterruptedException的方法通常属于此类.当然,调用链中的所有方法都需要以一种或另一种方式进行协作.

It will not stop the thread unless the methods executing in it are willing to terminate when they receive the "interrupt" signal. Methods that throw InterruptedException usually fall into this category. Of course, all methods in the call chain need to cooperate in one way or another.

如果您的代码未调用可中断"方法或其中的部分长时间不调用它们,则使它们定期检查标志Thread.isInterrupted(),并在其变为true时优雅地终止.如果您曾经收到(并应该抓住)InterruptedException s,也是同样的事情.

If your code does not call "interruptable" methods or has sections that don't call them for long periods, then make them check flag Thread.isInterrupted() periodically, and terminate clean and gracefully if it ever becomes true. Same thing if you ever receive (and catch, which you should) InterruptedExceptions.

但是,如果您的意图更具体,则可以改善您的问题.我正要解释线程中断"协议的工作原理,但是也许这不是您想要的.

Your question can be improved, though, if your intent is more specific. I was about to explain how the "thread interrupt" protocol works, but perhaps this is not what you are looking for.

这篇关于Java中的线程中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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