gpt4 book ai didi

Java 并发 - 中断策略

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

我正在阅读 Java Concurrency in Practice .在章节中断政策部分
取消和关闭
它提到

A task should not assume anything about the interruption policy of its executing thread unless it is explicitly designed to run within a service that has a specific interruption policy. Whether a task interprets interruption as cancellation or takes some other action on interruption, it should take care to preserve the executing thread's interruption status. If its not going to propagate InterruptedException to its caller, it should restore the interruption status after catching InterruptionException:Thread.currentThread().interrupt()


所以我试着玩弄 list 样本来理解。但我对输出感到困惑。
主要制作人
public class CorrectPrimeProducer extends Thread {

private final BlockingQueue<BigInteger> queue;

public CorrectPrimeProducer(BlockingQueue<BigInteger> queue) {
this.queue = queue;
}

@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+" interrupt status in producer:" + Thread.currentThread().isInterrupted());
BigInteger p = BigInteger.ONE;
while (!Thread.currentThread().isInterrupted()) {
queue.put(p = p.nextProbablePrime());
}
} catch (InterruptedException e) {
/* Allow thread to exit */
Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().getName()+" interrupt status in producer catch:" + Thread.currentThread().isInterrupted());
}
}
}
主要方法##
public static void main(String[] args) throws InterruptedException {
BlockingQueue<BigInteger> primes = new LinkedBlockingQueue<>();
CorrectPrimeProducer generator = new CorrectPrimeProducer(primes);
generator.start();
try {
while (needMorePrimes()) {
consume(primes.take());
}
} finally {
generator.interrupt();
}
TimeUnit.SECONDS.sleep(5);
System.out.println(generator.getName()+" interrupt status in main:"+generator.isInterrupted());
}

//do something
private static void consume(BigInteger take) {
System.out.println(take);
}

private static int counter = 1;

private static boolean needMorePrimes() {
counter++;
if(counter == 10){
// after counter reaches 10 return false
return false;
}
return true;
}
输出:
// when TimeUnit.SECONDS.sleep(5); in main class is not commented

Thread-0 interrupt status in producer:false
2
3
5
7
11
13
17
19
Thread-0 interrupt status in producer catch:true
Thread-0 interrupt status in main:false
//When TimeUnit.SECONDS.sleep(5); in main class is commented
Thread-0 interrupt status in producer:false
2
3
5
7
11
13
17
19
Thread-0 interrupt status in main:true
Thread-0 interrupt status in producer catch:true


  • 只需在主类的主线程中添加 TimeUnit.SECONDS.sleep(5) 即可。正在执行的线程(即生成器)中断状态正在重置。如果我评论 TimeUnit.SECONDS.sleep(5) 方法,那么在这种情况下会保留中断状态。为什么会发生这种情况以及如何发生?
  • 在书中它提到一个线程应该只被它的所有者中断。在上面的例子中,谁是所有者?我认为它的主要方法线程。
  • 最佳答案

    Just by adding TimeUnit.SECONDS.sleep(5) in main thread in main class. The executing thread (ie, generator) interrupt status is getting reset. If I comment the TimeUnit.SECONDS.sleep(5) method then in that case interrupt status is retained. Why is this happening and how ?


    您没有在主线程和 CorrectPrimeProducer 之间使用任何同步机制(除了阻塞队列)所以当主线程打印状态 - CorrectPrimeProducer可能还没有保留中断状态(通过执行 catch 块指令)因此你得到 false作为结果。
    当您添加 sleep到主 Thread你只是增加了 CorrectPrimeProducer的可能性线程通过调用 catch保持中断状态在主线程尝试打印其状态之前阻止指令。这就是为什么它会打印 true .

    In book it's mentioned A thread should be interrupted only by its owner . Here in the above example who is the owner ? I think its main method thread.


    在这种情况下,您是 CorrectPrimeProducer 的所有者(所有者是创建线程的代码)。线程,以便您决定中断对它意味着什么。例如,如果它被中断,您可以重新创建它(例如,默认情况下,Java 线程池中的 Thread 会发生这种情况)。

    关于Java 并发 - 中断策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63586688/

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