问题标签 [interrupted-exception]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 为什么在 Callable 中设置中断位
因此,此资源(http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html)建议在线程不处理中断本身时在线程中设置中断位,“这样调用堆栈上较高的代码就可以了解中断并在需要时对其进行响应。”
假设我正在使用 ExecutorService 在不同的线程中运行某些东西。我构造了一个 Callable 并将这个 Callable 传递给 ExecutorService.submit(),它返回一个 Future。如果 Callable 被中断然后重置中断位,则关联的 Future 在调用 Future.get() 时不会抛出 InterruptedException。那么如果这个 Future 是主线程访问生成的线程的唯一方式,那么在 Callable 中设置中断位的目的是什么。
java - 如果在睡眠线程上调用 interrupt() 会发生什么?
我有一个线程,run()
我打电话给sleep()
. 如果我中断这个线程会发生什么?
我想澄清以下几点:
- 如果线程还没有启动,那么调用
interrupt()
什么都不会做,对吧? - 如果线程已启动,并且现在正在休眠,
interrupt()
则在休眠时调用将抛出InterruptedException
; 因此,转到catch()
然后结束线程,对吗?
java - Thread.sleep() 阻塞主 UI 线程
我有这个线程:
然后我开始了线程。但是线程阻塞了主 UI 线程,导致它不响应用户交互。
java - 通过静态分析识别错误处理的 InterruptedExceptions
有没有办法识别(通过静态分析,例如 findbugs 甚至可能是 checkstyle)何时未正确处理 InterruptedException(例如将线程标记为中断或抛出异常或可选:有注释解释原因。后一个选项可以忽略吗?)
java - 为什么 InterruptedException 是已检查异常?
当我们制作自己的线程时,这是关于旧 Java 的一个问题。一些方法,例如当它被另一个线程中断时Thread.sleep(100)
抛出一个。InterruptedException
现在据我了解,中断意味着另一个线程在说:让我现在接手。
当这种情况发生时,为什么Java要我们处理InterruptedException
?
程序员甚至不需要关心线程何时相互中断。他应该能够在线程之间分配工作并在完成时收到通知。那么Java要我们处理的原因是什么InteruptedException
呢?至少应该是RuntimeException
java - 中断异常发生原因
notifyAll()
在查看Object 类下的方法的 javadoc 时,出现了以下几行:
如果当前线程在等待之前或期间被任何线程中断,则抛出 InterruptedException。在此对象的锁定状态已按上述恢复之前,不会引发此异常。
重点是:
当前线程在等待时被中断
这是什么意思?线程在等待时可以中断吗?如果是,为什么?它有什么用?
java - 为什么这里抛出中断异常...原因?
我只是在这里感到困惑,为什么t2.interrupt()
当 t2 正在等待获取资源对象的锁定时不抛出异常,并且interrupt()
方法可能会抛出安全异常,那么为什么编译器仍然允许我们执行它而不将其放入 try catch 块中。
java - 使用中断方法
在第 7 步(正如我所标记的),主线程调用interrupt()
thread t2
,但由于它正在等待获取资源的锁,它不会引发任何异常。之后,主线程End Main
在等待 1000 ns 后打印“”。换句话说,主线程已经完成了它的任务,那么什么会t2.interrupt()
因为它抛出异常而再次触发呢?
java - Why should we not swallow the InterruptedException
I am very confused and not able to understand why InterruptedException should not be swallowed.
The article from IBM says
When a blocking method detects interruption and throws InterruptedException, it clears the interrupted status. If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred so that code higher up on the call stack can learn of the interruption and respond to it if it wants to
Also,Java Concurrency in Practice discusses this in more detail in Chapter 7.1.3: Responding to Interruption. Its rule is:
Only code that implements a thread's interruption policy may swallow an interruption request. General-purpose task and library code should never swallow interruption requests.
1.Can anyone explain how can code in higher call stack make use of the status set by Thread.currentThread().interrupt(); in catch block when the thread is terminated?
Also Please explain the above rule?
java - Java 监视器——捕获 InterruptedException
我有一个监视器的java实现,使用
我要解决的问题是读者/作者问题。我有一个锁lock
和两个条件readers
和writers
。
我注意到Condition.await()
函数 throws InterruptedException
。现在,我刚刚用 try / catch 块包围了该方法。但是,catch 块是空的。
我想知道什么时候抛出这个异常以及我应该如何处理它。
readers.await()
当有写入文件的写入者/有写入文件的写入者等待写入文件时调用。
writers.await()
当有一个或多个读取器从文件读取或写入器当前正在写入文件时调用。
什么情况下会抛出InterruptedException,应该如何处理?