我有一个具有传入作业队列的线程(包含作业描述的LinkedList
)。当没有工作要做时,线程在队列上阻塞wait()
。当外部作业调度程序对象在队列中放置新作业时,会使用notify()
唤醒它。
关闭我的程序时,我在线程上调用interrupt()
。当线程等待InterruptedException
中的作业时,这会引发wait()
。我的问题是:如果我在没有阻塞但是做某种工作的情况下中断线程会发生什么,处理后的项目是队列中的最后一个(所以队列现在是空的)并且执行过去isInterrupted()
检查之前设置中断标志,以便再次调用wait()
?它会抛出一个InterruptedException
,因为已经设置了中断标志或者线程永远等待,因为新的作业永远不会到达队列而且没有人可以中断等待吗?
答案 0 :(得分:11)
中断此主题。
除非当前线程正在中断,否则总是如此 允许,调用此线程的checkAccess方法,这可能是 导致抛出SecurityException。
如果在调用wait()时阻塞了这个线程,请等待(long), 或者等待(long,int)Object类或join()的方法, join(long),join(long,int),sleep(long)或sleep(long,int)方法 这个类,然后它的中断状态将被清除 收到InterruptedException。
如果此线程在可中断的I / O操作中被阻止 然后通道将关闭,线程的中断状态 将被设置,线程将收到ClosedByInterruptException。
如果此线程在Selector中被阻塞,那么线程的中断 状态将被设置,它将立即从选择中返回 操作,可能具有非零值,就像选择器一样 唤醒唤醒方法。
如果以前的条件都没有,则该线程的中断 状态将被设置。
中断非活动的线程无需任何效果。
所以,所有这一切都将是线程的中断状态将被设置。
如果您阅读Object.wait()
的API规范,则会看到以下内容 -
抛出:
InterruptedException - 如果任何线程在当前线程等待a之前或当前线程中断当前线程 通知即可。抛出此异常时,将清除当前线程的中断状态。
答案 1 :(得分:9)
是的,你的被中断的线程在调用wait()时会抛出InterruptedException。这对你自己来说非常简单。
public class TestInt {
public static void main(String[] args) throws Exception
{
Thread.currentThread().interrupt();
synchronized(TestInt.class) {
TestInt.class.wait();
}
}
}
另外,请注意Object.wait()
的javaodc:
InterruptedException - 如果任何线程在之前或之后中断当前线程 当前线程正在等待通知。当前中断状态 抛出此异常时清除线程。
答案 2 :(得分:1)
虽然这是一个很老的问题,但我想补充一点,如果您在 Thread.currentThread().interrupt()
期间调用 lock.wait()
方法(而线程处于 WAITING
状态),它不会意味着线程将在内部立即抛出 InterruptedException
,第一个线程将进入 BLOCKED
状态(它被唤醒,就像来自另一个线程的 lock.notify()/notifyAll()
调用一样,这允许它竞争共享锁) 将中断标志设置为 true,可能在一段时间后仅在再次获取锁定后(有时相当长的时间)它可以抛出一个 InterruptedException
(没有锁定它无法继续,甚至处理异常)。
package com.company;
public class Main {
public static void main(String[] args) {
final Object lock = Main.class;
// three simple threads
Thread th1 = new Thread(() -> {
synchronized (lock) {
for (int i = 0; i < 15; i++) {
sleepWithoutException(100);
System.out.println("th1 is doing some job!");
}
}
});
Thread th2 = new Thread(() -> {
synchronized (lock) {
for (int i = 0; i < 15; i++) {
sleepWithoutException(100);
System.out.println("th2 is doing some job!");
}
}
});
Thread th3 = new Thread(() -> {
synchronized (lock) {
for (int i = 0; i < 15; i++) {
sleepWithoutException(100);
System.out.println("th3 is doing some job!");
}
}
});
// thread which will be in WAITING state after 1 second
Thread th4 = new Thread(() -> {
synchronized (lock) {
try {
sleepWithoutException(500);
lock.wait();
System.out.println("Continued");
} catch (InterruptedException e) {
System.out.println("interrupted! " + e);
System.exit(1);
}
}
});
// execution
th4.start();
sleepWithoutException(100);
th1.start();
th2.start();
th3.start();
// thread which will monitor states of other threads
new Thread(() -> {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000_000; i++) {
sleepWithoutException(100);
System.out.println("-------------------------------------------------");
System.out.println("step: " + i + " , timeElapsed: " + (System.currentTimeMillis() - start));
if (i == 10) {
th4.interrupt();
}
System.out.println(
th1.getState() + " " + th1.isInterrupted() + "\n" +
th2.getState() + " " + th2.isInterrupted() + "\n" +
th3.getState() + " " + th3.isInterrupted() + "\n" +
th4.getState() + " " + th4.isInterrupted()
);
}
}).start();
}
public static void sleepWithoutException(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
System.out.println("fallen in sleep" + e);
}
}
}
请参阅下面的日志,因为您可以看到其他线程在 BLOCKING
状态且标志 isInterrupted
的 th4 设置为 true 时继续工作(在调用 Thread.currentThread().interrupt()
方法之后)>
-------------------------------------------------
step: 0 , timeElapsed: 101
BLOCKED false
BLOCKED false
BLOCKED false
TIMED_WAITING false
-------------------------------------------------
step: 1 , timeElapsed: 201
BLOCKED false
BLOCKED false
BLOCKED false
TIMED_WAITING false
-------------------------------------------------
step: 2 , timeElapsed: 301
BLOCKED false
BLOCKED false
BLOCKED false
TIMED_WAITING false
-------------------------------------------------
step: 3 , timeElapsed: 401
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 4 , timeElapsed: 502
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 5 , timeElapsed: 602
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 6 , timeElapsed: 703
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 7 , timeElapsed: 803
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 8 , timeElapsed: 904
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 9 , timeElapsed: 1004
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING false
th3 is doing some job!
-------------------------------------------------
step: 10 , timeElapsed: 1105
BLOCKED false
BLOCKED false
TIMED_WAITING false
WAITING true
th3 is doing some job!
-------------------------------------------------
step: 11 , timeElapsed: 1205
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 12 , timeElapsed: 1306
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 13 , timeElapsed: 1406
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 14 , timeElapsed: 1507
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 15 , timeElapsed: 1607
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 16 , timeElapsed: 1708
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 17 , timeElapsed: 1808
BLOCKED false
BLOCKED false
TIMED_WAITING false
BLOCKED true
th3 is doing some job!
-------------------------------------------------
step: 18 , timeElapsed: 1909
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 19 , timeElapsed: 2009
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 20 , timeElapsed: 2110
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 21 , timeElapsed: 2211
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 22 , timeElapsed: 2311
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 23 , timeElapsed: 2412
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 24 , timeElapsed: 2512
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 25 , timeElapsed: 2613
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 26 , timeElapsed: 2713
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 27 , timeElapsed: 2814
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 28 , timeElapsed: 2914
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 29 , timeElapsed: 3015
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 30 , timeElapsed: 3115
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 31 , timeElapsed: 3216
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 32 , timeElapsed: 3316
BLOCKED false
TIMED_WAITING false
TERMINATED false
BLOCKED true
th2 is doing some job!
-------------------------------------------------
step: 33 , timeElapsed: 3417
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 34 , timeElapsed: 3517
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 35 , timeElapsed: 3618
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 36 , timeElapsed: 3718
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 37 , timeElapsed: 3819
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 38 , timeElapsed: 3919
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 39 , timeElapsed: 4019
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 40 , timeElapsed: 4120
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 41 , timeElapsed: 4220
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 42 , timeElapsed: 4321
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 43 , timeElapsed: 4421
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 44 , timeElapsed: 4522
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 45 , timeElapsed: 4622
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 46 , timeElapsed: 4723
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
-------------------------------------------------
step: 47 , timeElapsed: 4823
TIMED_WAITING false
TERMINATED false
TERMINATED false
BLOCKED true
th1 is doing some job!
interrupted! java.lang.InterruptedException
Process finished with exit code 1