- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
This IBM developerWorks article状态:
“The one time it is acceptable to swallow an interrupt is when you know the thread is about to exit. This scenario only occurs when the class calling the interruptible method is part of a
Thread
, not aRunnable
[…]”.
我现在总是为我的线程实现Runnable
。像这样提供 Runnable
实现:
public class View() implements Runnable {
@Overload
public void run(){
Thread worker = new Thread(new Worker());
worker.start();
do{
try{
TimeUnit.SECONDS.sleep(3);
updateView();
}catch(InterruptedException e){
worker.interrupt();
// Thread.currentThread().interrupt();
return;
}
}while(true);
}
protected void updateView(){
// …
}
}
真的有必要在我的 return;
语句之前调用 Thread.currentThread().interrupt();
吗? return;
不是已经执行了一次干净的退出了吗?调用它有什么好处?文章指出应该这样做,否则“[...] 调用堆栈上层的代码将无法找到它 [...]”。在应用程序关闭时,Thread.State.TERMINATED
中设置了中断标志的线程比没有中断标志的线程有什么好处?你能给我一个例子吗?Runnable
之外的代码出于合理的原因检查中断标志?
顺便说一句,扩展 Thread
而不是实现 Runnable
是更好的代码设计吗?
最佳答案
它重置中断标志。这JavaSpecialists newsletter更详细地涵盖了这个令人困惑的话题。
In my example, after I caught the InterruptedException, I used Thread.currentThread().interrupt() to immediately interrupted the thread again. Why is this necessary? When the exception is thrown, the interrupted flag is cleared, so if you have nested loops, you will cause trouble in the outer loops
所以如果你知道你的代码不会被另一个组件使用,那么你就不需要重新中断。但是我真的不会做那个小的优化。谁知道您的代码将来将如何使用/重用(甚至通过复制/粘贴),因此我会为每个中断重置标志。
关于java - 始终调用 Thread.currentThread().interrupt();捕获 InterruptedException 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20093845/
我在我的操作系统内核中编写了一些函数来在指令计数器溢出时发出性能监控中断 (PMI)。它在我的机器(Intel core i5)上运行良好。但是当我使用 在 qemu 上运行它时 qemu-syste
这个问题已经有答案了: What does java.lang.Thread.interrupt() do? (10 个回答) 已关闭10 年前。 从设置线程状态的角度,我对这两种方法不太清楚。 Ja
使用 LPTIM1 中断处理程序,我的代码设置为在进入处理程序后立即清除中断标志,但在调试期间,“中断清除寄存器”(ICR) 中的标志未设置为 0,因此程序直接跳转到由于在“中断和状态寄存器”(ISR
我还没有找到任何关于 MC68000 在外部中断期间是否保留其状态寄存器/CCR 的信息。我想如果 CPU 在条件分支之前立即中断,并且中断修改了 CCR,这将是一个问题。现在我的中断代码如下所示:
我需要弄清楚为什么我在 Internet 上找到的每个关于如何从 Runnable#run() 方法中捕获 interrupt() 的示例,它们看起来都是像这样: while (Thread.curr
运行下面的代码后,我收到标题中的错误。 #include int main() { printf("hello World!\n"); retu
我想知道 Thread.interrupt() 和 Thread.currentThread.interrupt() 是否做同样的事情或者会给出相同的结果?如果不是,有什么区别? 类似的问题是:Thr
我正在阅读 Java Concurrency in Practice 并遇到以下代码片段。 public static void timedRun(final Runnable r,
在嵌入式项目中,我需要使用同一个 IRQ 处理程序处理两个不同的外部中断。我不想直接更改启动程序集中的中断 vector 表条目,因此我必须坚持使用 IRQ 处理程序例程(EXTI9_5_IRQHan
的教育原则是:没有愚蠢的问题。这背后的基本思想是人们通过提问来学习。 我被要求:“你能在编程层面展示和解释如果每个任务都可以执行所有指令会发生什么坏事吗?” 我确实给了代码 main(){
我正在为 VM 的 IO 进行 KVM 优化。我看过KVM代码,通常所有的物理中断都会导致VMexit进入KVM。然后主机的IDT会处理相应的物理中断。我的问题是 KVM 如何决定是否向 guest
我正在尝试做这样的事情 $SIG{ALRM} = sub { print $line_number_when_alarm_went_off; }; alarm 10; # rest of th
首先,我应该分享我所知道的一切-那完全是混乱。关于这个主题有几个不同的问题,所以请不要烦恼:)。 1)为了找到一个ISR,CPU提供了一个中断号。在x86机器(286/386及更高版本)中,有一个带有
我有一个关于linux系统调用的基本问题。 为什么系统调用不像正常的函数调用那样处理,而是通过软件中断处理? 是不是因为在用户应用程序的构建过程中,没有为用户空间应用程序与内核执行链接过程? 最佳答案
我使用的是stm32f0 MCU。 我有一个简单的 UART 回显代码,其中接收到的每个字节都将被发送出去。我测试过它有效。在这里; uint8_t Rx_data[5]; uint32_t tx_t
是否有专门针对 msp430F5438A 的中断服务例程 (ISR) 名称的完整列表?任何帮助将不胜感激,谢谢。 最佳答案 您可以在 msp430/include/msp430f5438a 中找到 #
是否有专门针对 msp430F5438A 的中断服务例程 (ISR) 名称的完整列表?任何帮助将不胜感激,谢谢。 最佳答案 您可以在 msp430/include/msp430f5438a 中找到 #
中断线程 线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。
我正在开发一个操作系统项目,使用 isolinux (syslinux 4.5) 作为引导加载程序,加载我的内核与组织在 0x200000 的多重引导头文件。 据我所知,内核已经处于 32 位保护模式
我正在处理一个启动时间要求很紧的项目。目标架构是在 32 位保护模式下运行的基于 IA-32 的处理器。确定可以改进的领域之一是当前系统动态初始化处理器的 IDT(中断描述符表)。由于我们没有任何即插
我是一名优秀的程序员,十分优秀!