gpt4 book ai didi

java - 使用Thread.currentThread()。isInterrupted()与Thread.sleep()

转载 作者:行者123 更新时间:2023-12-03 13:04:37 26 4
gpt4 key购买 nike

我有一个线程调用work()方法,这是一个简单的while(true)-loop,如下所示;

public void work() throws InterruptedException {
while (true) {
// PART A
if (Thread.currentThread().isInterrupted()) {
System.out.println("....run()::work::PART A::isInterrupted():" + Thread.currentThread().isInterrupted());
Thread.sleep(2000);
}

// Is it possible to make the below code executable at runtime when thread is interrupted?
// PART B
if (Thread.currentThread().isInterrupted()) {
System.out.println("....run()::work::PART B::isInterrupted():" + Thread.currentThread().isInterrupted());
Thread.sleep(2000);
}
}
}

我运行了该线程实例,并在一段时间后使用Thread.sleep()方法从主线程中中断了线程。当我更改延迟时,当线程从主线程中断时,我希望能打到注释的部分,但我只会看到下面的输出;
....run()::Work::starting
.main()::interrupting thread t
.main()::leaving
....run()::work::PART A::isInterrupted():true
....run()::Work::ERROR::INTERRUPTED

我的简单问题是, 是否可以执行work()方法的第二部分(B部分),否则,为什么?

完整的演示如下。
public class GeneralInterrupt implements Runnable {
public void run() {
try {
System.out.println("....run()::Work::starting");
work();
System.out.println("....run()::Work::ended");
} catch (InterruptedException x) {
System.out.println("....run()::Work::ERROR::INTERRUPTED");
return;
}
System.out.println("....run()::Work::leaving normally");
}

public void work() throws InterruptedException {
while (true) {
// PART A
if (Thread.currentThread().isInterrupted()) {
System.out.println("....run()::work::PART A::isInterrupted():" + Thread.currentThread().isInterrupted());
Thread.sleep(2000);
}

// Is it possible to make the below code executable at runtime when thread is interrupted?
// PART B
if (Thread.currentThread().isInterrupted()) {
System.out.println("....run()::work::PART B::isInterrupted():" + Thread.currentThread().isInterrupted());
Thread.sleep(2000);
}
}
}

public static void main(String[] args) {
Thread t = new Thread(new GeneralInterrupt());
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException x) {
}
System.out.println(".main()::interrupting thread t");
t.interrupt();
System.out.println(".main()::leaving");
}
}

另请注意,如果同时从A和B部分删除了“Thread.sleep(2000)”行,则输出为
....run()::work::PART B::isInterrupted():false
....run()::work::PART B::isInterrupted():false
....run()::work::PART B::isInterrupted():false
....run()::work::PART B::isInterrupted():false
....run()::work::PART A::isInterrupted():true
....run()::work::PART A::isInterrupted():true
....run()::work::PART A::isInterrupted():true
....run()::work::PART A::isInterrupted():true
....run()::work::PART A::isInterrupted():true

谁能解释为什么Thread.sleep()方法在work()方法中表现出这种方式。

最佳答案

按照Oracle Tutorial的说法,中断线程是向线程发出信号,以结束其当前工作并执行其他操作(例如终止):

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.



因此,通常应该可以在中断后继续。

按照 JavaDoc上的中断,它取决于线程在哪里中断:

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. (emphasis added by me)



这就是为什么在使用 sleep(2000)时会得到异常的原因。

关于java - 使用Thread.currentThread()。isInterrupted()与Thread.sleep(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31020599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com