主线程 sleep/interrupt 如何在以下代码中工作
How the main thread sleep/interrupt works in following code
第二个循环如何真正打断休眠的主线程,而第一个没有??
我的理解是Thread.sleep(3000)之后,代码Thread.currentThread().interrupt()会在3秒后执行。
谁能解释一下它是如何工作的
for (int i = 0; i < 2; i++) {
try {
System.out.println("loop : " + i);
Thread.sleep(3000);
System.out.println("Woke up");
Thread.currentThread().interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
loop : 0
Woke up
loop : 1
java.lang.InterruptedException: sleep interrupted
exception loop:1
at java.base/java.lang.Thread.sleep(Native Method)
at multithreadings.Mainclass.main(Mainclass.java:13)
您发布的代码涉及一个线程。该线程执行一个循环。在第一次迭代中,线程会自行中断。中断并不意味着"stop executing immediately"。意思是:"please, I would like you to stop running when you can".
想要尊重中断请求的线程可以通过两种方式实现:
- 它定期检查是否被中断,如果是,则停止执行(例如,通过跳出循环或返回)
- 它调用了一个阻塞方法,例如
sleep()
,如果线程已经被中断或正在被中断,它会抛出一个InterruptedException。
发生的是第二种情况。中断请求在调用 sleep() 之后的第一次迭代中完成。线程继续运行,在第二次迭代时,调用sleep(),抛出InterruptedException,因为线程之前已经被中断了。
阅读文档,即interrupt()
的javadoc:
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, ...
If this thread is blocked ...
If this thread is blocked ...
If this thread is blocked ...
If none of the previous conditions hold then this thread's interrupt status will be set.
Throws InterruptedException
if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
由于线程已经在循环的第二次迭代中中断了自身,因此设置了自己的中断状态,sleep(...)
方法立即中断。
中断是一种礼貌的停止请求:线程没有义务停止。
这就像罗宾·威廉姆斯关于 what police in the UK say when you commit a crime 的笑话:
Stop! Or I'll say stop again!
此外,中断线程不会导致抛出 InterruptedException
:它只是在线程上设置一个标志。如果某些东西(比如 Thread.sleep
)检查了这个标志,并发现它被设置了,它可能会抛出一个 InterruptedException
;但是标志和异常是表示中断的两种正交方式。
因此:
- 第一次执行时,休眠3秒,然后设置中断标志,循环体正常结束。
- 第二次执行时,你要求休眠3秒,但
Thread.sleep
检测到中断标志,并抛出异常。
在 Java 中,一个 线程 无法停止另一个线程。一个线程只能请求另一个线程停止。请求以中断的形式发出。在 Thread 实例上调用 interrupt() 方法会将实例的中断状态设置为 true。
一旦中断状态设置为true.,所有阻塞方法都会通过抛出InterruptedException来响应中断,如果您想查看更详细的信息,请阅读下面的文章。查看 Thread Interruption 在 Java 中的工作原理。
第二个循环如何真正打断休眠的主线程,而第一个没有?? 我的理解是Thread.sleep(3000)之后,代码Thread.currentThread().interrupt()会在3秒后执行。 谁能解释一下它是如何工作的
for (int i = 0; i < 2; i++) {
try {
System.out.println("loop : " + i);
Thread.sleep(3000);
System.out.println("Woke up");
Thread.currentThread().interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
loop : 0
Woke up
loop : 1
java.lang.InterruptedException: sleep interrupted
exception loop:1
at java.base/java.lang.Thread.sleep(Native Method)
at multithreadings.Mainclass.main(Mainclass.java:13)
您发布的代码涉及一个线程。该线程执行一个循环。在第一次迭代中,线程会自行中断。中断并不意味着"stop executing immediately"。意思是:"please, I would like you to stop running when you can".
想要尊重中断请求的线程可以通过两种方式实现:
- 它定期检查是否被中断,如果是,则停止执行(例如,通过跳出循环或返回)
- 它调用了一个阻塞方法,例如
sleep()
,如果线程已经被中断或正在被中断,它会抛出一个InterruptedException。
发生的是第二种情况。中断请求在调用 sleep() 之后的第一次迭代中完成。线程继续运行,在第二次迭代时,调用sleep(),抛出InterruptedException,因为线程之前已经被中断了。
阅读文档,即interrupt()
的javadoc:
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, ...
If this thread is blocked ...
If this thread is blocked ...
If this thread is blocked ...
If none of the previous conditions hold then this thread's interrupt status will be set.
Throws
InterruptedException
if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
由于线程已经在循环的第二次迭代中中断了自身,因此设置了自己的中断状态,sleep(...)
方法立即中断。
中断是一种礼貌的停止请求:线程没有义务停止。
这就像罗宾·威廉姆斯关于 what police in the UK say when you commit a crime 的笑话:
Stop! Or I'll say stop again!
此外,中断线程不会导致抛出 InterruptedException
:它只是在线程上设置一个标志。如果某些东西(比如 Thread.sleep
)检查了这个标志,并发现它被设置了,它可能会抛出一个 InterruptedException
;但是标志和异常是表示中断的两种正交方式。
因此:
- 第一次执行时,休眠3秒,然后设置中断标志,循环体正常结束。
- 第二次执行时,你要求休眠3秒,但
Thread.sleep
检测到中断标志,并抛出异常。
在 Java 中,一个 线程 无法停止另一个线程。一个线程只能请求另一个线程停止。请求以中断的形式发出。在 Thread 实例上调用 interrupt() 方法会将实例的中断状态设置为 true。
一旦中断状态设置为true.,所有阻塞方法都会通过抛出InterruptedException来响应中断,如果您想查看更详细的信息,请阅读下面的文章。查看 Thread Interruption 在 Java 中的工作原理。