当前位置: 首页 > 面试题库 >

清除Thread.interrupt()标志的方法

高墨一
2023-03-14
问题内容

我最近继承了一个大型Java应用程序,其中几乎没有线程安全性。我目前正在做的是让所有线程正确处理被中断,而不是使用非常糟糕的线程Thread.stop()

问题的部分原因是,我不知道在那里清除中断标志的每个方法调用。

目前,我知道以下内容将清除中断标志:

Thread.interrupted()
Thread.sleep(long)
Thread.join()
Thread.join(long)
Object.wait()
Object.wait(long)

我还想念什么?谢谢


问题答案:

问题的部分原因是,我不知道在那里清除中断标志的每个方法调用。

重要的是要阐明以下方法仅通过调用它们来清除中断标志:

Thread.interrupted()
Thread.isInterrupted(true) -- added to your list

因此,Thread.currentThread().isInterrupted()应始终使用它代替。

下列方法将通过 立即 抛出中断标志来清除中断标志:InterruptedException如果它们被调用然后被中断, 或者 线程 已经
被中断然后被调用(请参阅下面的junit代码)。因此,不是清除标志而是抛出异常的方法。

您的初始列表:

Thread.interrupted()
Thread.sleep(long)
Thread.join()
Thread.join(long)
Object.wait()
Object.wait(long)

添加到您的列表:

Thread.sleep(long, int)
Thread.join(int, long)
Thread.isInterrupted(true)
Object.wait(int, long)
BlockingQueue.put(...)
BlockingQueue.offer(...)
BlockingQueue.take(...)
BlockingQueue.poll(...)
Future.get(...)
Process.waitFor()
ExecutorService.invokeAll(...)
ExecutorService.invokeAny(...)
ExecutorService.awaitTermination(...)
CompletionService.poll(...)
CompletionService.take(...)
CountDownLatch.await(...)
CyclicBarrier.await(...)
Semaphore.acquire(...)
Semaphore.tryAcquire(...)
Lock.lockInteruptibly()
Lock.tryLock(...)

请注意 ,捕获到的 任何
代码的正确模式InterruptedException是立即重新中断线程。如果其他人依赖该thread.isInterrupted()方法,我们会这样做:

try {
    ...
} catch (InterruptedException e) {
    // immediately re-interrupt the thread
    Thread.currentThread().interrupt();
    // log the exception or [likely] quit the thread
}

JUnit代码演示了其中的一些功能:

assertFalse(Thread.currentThread().isInterrupted());
// you can do this from another thread by saying: someThread.interrupt();
Thread.currentThread().interrupt();
// this method does _not_ clear the interrupt flag
assertTrue(Thread.currentThread().isInterrupted());
// but this one _does_ and should probably not be used
assertTrue(Thread.interrupted());
assertFalse(Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
assertTrue(Thread.currentThread().isInterrupted());
try {
    // this throws immediately because the thread is _already_ interrupted
    Thread.sleep(1);
    fail("will never get here");
} catch (InterruptedException e) {
    // and when the InterruptedException is throw, it clears the interrupt
    assertFalse(Thread.currentThread().isInterrupted());
    // we should re-interrupt the thread so other code can use interrupt status
    Thread.currentThread().interrupt();
}
assertTrue(Thread.currentThread().isInterrupted());


 类似资料:
  • 我最近继承了一个大型Java应用程序,其中几乎没有线程安全性。我目前正在做的是让所有线程正确处理被中断的情况,而不是使用非常糟糕的。 部分问题是,我不知道清除中断标志的每个方法调用。 目前我知道以下操作将清除中断标志: 我还缺什么?非常感谢。

  • 本文向大家介绍mysql清除log-bin日志的方法,包括了mysql清除log-bin日志的方法的使用技巧和注意事项,需要的朋友参考一下 前一阵子工作项目上的事情忙的焦头烂额,最近要进行部门调整将要去做新的项目。又要学习很多新的知识了,还是很兴奋激动的。今天下班回来查看了一下VPS状态,发现VPS的空间只剩下了1G多!第一反应是被入侵了,但是看了一下log并没有发现什么异常的登录,加上平时基本都

  • 标记-清除是主流的追踪式收集算法的一种,和引用计数相比,追踪式收集是一种间接方法,与程序耦合性很低,简单地说,就是程序在需要的时候只管申请内存,使用时也不用像引用计数机制要做实时地维护,直到一定条件(一般是已申请内存空间达到一定阈值),进程暂停执行,由垃圾收集器回收垃圾,然后继续执行,当然如果收集之后还是内存不足,就报错。很多语言的主流实现都使用追踪式收集,比如java,C#等 追踪式收集严格按照

  • 我有一个 A 的任务堆栈 我正在这样设置我的吊挂帐篷。有什么明显的问题吗? 编辑1: 我尝试了这里的建议:清除任务中的所有活动? 但是我仍然得到相同的结果。我的活动A从我的应用程序任务堆栈中开始,我按回并转到C,然后是B,然后又是A。 我开始认为这在Android中是不可能的,或者在使用待定的意图时是不可能的。 编辑2:这不是需要什么标志的问题。更大的问题是什么可能会出错,这些标志似乎没有任何效果

  • 我会很感激任何关于在哪里搜索这些文件的提示。

  • 本文向大家介绍bootstrap清除浮动的方法?相关面试题,主要包含被问及bootstrap清除浮动的方法?时的应答技巧和注意事项,需要的朋友参考一下 :after伪类在元素末尾插入了一个包含空格的字符,并设置display为table display:table会创建一个匿名的table-cell,从而触发块级上下文(BFC),因为容器内float的元素也是BFC,由于BFC有不能互相重叠的特性