- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下代码:
public void run(){
while (true){
function1();
...
functionN();
}
}
我想“优雅地”退出——这对我来说意味着一旦我发送了一个关闭信号并且当前线程在 functionK() 处,线程将“中断”循环并退出运行。
所以我试过像这样使用 Thread.interrupt():
public void run(){
while (true){
try {
function1();
...
functionN();
} catch (InterruptedException ex) {
/* Cleanup and exit. */
}
}
}
但这是行不通的 - 即使中断标志打开,线程也会继续无休止地运行。
仅作记录:
public void run(){
while (!thread.isInterrupted()){
try {
function1();
...
functionN();
} catch (InterruptedException ex) {
/* Cleanup and exit. */
}
}
}
停止循环,但对我没有帮助。由于每个函数执行的操作可能需要几分钟,并且有很多不同的函数,因此在每个函数之前检查中断标志是否是一个可能代价高昂(特别是因为大多数时候应用程序运行平稳)。
我想知道是否有一种特殊的机制可以用来解决这类问题。
最佳答案
API documentation对此非常清楚:
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.
If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
因此,如果您正在等待对象监视器,则只能依赖此异常。某些 I/O 操作会抛出一些其他异常,但如果您也不使用它们,那么除了检查 interrupted()
标志外别无选择。
但您可以做的是重新组织您的代码:如果您有一个接一个调用的 N
方法,是否不可能将它们抽象成一个循环?通常可以找到重构代码以支持中断的方法,具体方法取决于您的实际场景。我的第一个问题是:为什么一个方法运行几分钟?这听起来有点可疑(尽管它可能是合理的)。
无论哪种方式,可中断性都不是免费的,如果您希望代码对中断的响应快于主循环的长度,则必须积极设计中断点。
不过还有一件事:检查 interrupted()
标志绝对不会代价高昂。当您在主循环中花费几分钟时不会,而且它比构造和处理异常要便宜得多。我什至会说,您会发现没有什么比调用 Thread.isInterrupted()
更快的了。
关于java - 在主循环内中断线程 'gracefully',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142284/
在设置我的 default.vcl 文件 (Varnish 3) 时,我看到了对 vcl_fetch 使用 beresp.grace 或 obj.grace 的不同建议。有什么区别? https://
假设我有以下代码: public void run(){ while (true){ function1(); ... functionN();
我正在开发一个在 Linux 环境中作为守护进程运行的 Python 脚本。如果以及当我需要对设备发出关机/重启操作时,我想做一些清理并将数据记录到文件中以在关机期间保留它。 我四处查看有关 Linu
现在我正在使用 Process.Kill() 来终止进程。有没有办法,而不是立即杀死它,我可以向进程发送一条消息,指示它关闭,以便它可以优雅地清理和关闭。基本上,我正在寻找与单击右上角的红色 X 等效
收到消息Your password will be expired with in 7 days后,我将default配置文件的密码过期天数更改为UNLIMITED。但是某些用户的帐户状态仍保留在EX
我正在尝试 Docker 并遇到docker container stop # Gracefully stop the specified container 我不是在问docker stop和do
我有一个后台 worker 实现了 BackgroundService(由 MS 提供)。 看看这个简单的实现: public class MyService : BackgroundService
我全新安装了 Slackware 64 14 位,并查看了 Varnish installation documentation我安装了所有依赖项并编译了 varnish,没有错误(make chec
使用 D7 + Indy 10 最新版本。 我的代码使用 TIdSMTP 发送电子邮件。我不断收到一些最终用户的“连接正常关闭”的消息,但电子邮件从未发送。 代码如下: try ~~~~ ~~~~ _
如果我们在后台启动 KafkaStream 应用程序(比如 Linux),是否有办法从外部向应用程序发送信号,以启动正常关闭? 最佳答案 如文档 ( https://kafka.apache.org/
如果我们在后台启动 KafkaStream 应用程序(比如 Linux),是否有办法从外部向应用程序发送信号,以启动正常关闭? 最佳答案 如文档 ( https://kafka.apache.org/
我正在开发我的第一个多线程项目,因此有一些我不确定的事情。有关我的设置的详细信息位于 previous question简而言之:我有一个由 Executors.newFixedThreadPool(
目标:拥有带有嵌入式 Jetty 的 Spring Web 应用程序,我想正常关闭/重新启动该应用程序。 这是 EmbeddedServletContainerFactory bean(如果需要,我将
我已经配置了 grunt,当我运行“grunt serve”时,我得到这个错误: Error: Cannot find module 'graceful-fs' at Function.Mod
在等待 boost::process::child 时,您如何知道它是否“优雅地”退出? 假设我创建了一个流程: boost::process::child child( "myprg.exe", "
是 import multiprocessing import schedule def worker(): #do some stuff def sched(argv): sche
为冗长的介绍道歉 - 只是试图适本地设置场景以避免歧义和困惑。 Web 应用程序已经从用于服务器端逻辑的基于浏览器的笨接口(interface)发展为完全基于浏览器的应用程序,对服务器端逻辑的需求最少
我正在编写一个应用程序,希望客户能够直接从浏览器上传到 Amazon S3。我可以把这个工作做得很好。但是当错误发生时,我希望能够更优雅地处理它们,而不是将 XML 文档散布在客户的屏幕上。 我有一个
我正在观看有关 Datastax 的 DS201 类(class),讲师突然开始使用术语“GC GRACE”。 “GC Grace”和“gc_grace_seconds”究竟是什么? 最佳答案 'gc
我正在尝试找到一种方法来模拟set -e在函数中的行为,但仅在该函数的范围内。 基本上,我想要一个函数,如果有任何简单的命令将触发set -e,它将返回1的上一级。目的是将有风险的工作集隔离为功能,以
我是一名优秀的程序员,十分优秀!