gpt4 book ai didi

java 并发 - 中断 DeadLook 线程

转载 作者:行者123 更新时间:2023-11-29 05:56:44 26 4
gpt4 key购买 nike

我正在尝试学习 java 并发的基础知识,我从 oracle java 教程开始直到 http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html ,我只是在代码示例中添加了一些行,但是当我运行代码时,main 方法永远不会返回。 我知道 alphonseThread 和 gastonThread 都被 ether 死锁了,但是为什么不能从主线程中中断它们呢?

public class DeadLock {

static class Freind {

private final String name;
public Freind(String name_) {
this.name = name_;
}

public String getName() {
return this.name;
}

public synchronized void bow(Freind bower) throws InterruptedException {
System.out.format("%s : %s " + "has bowed to me!%n",
this.name, bower.getName());
Thread.sleep(2000);
bower.bowBack(this);

}

public synchronized void bowBack(Freind bower) {
System.out.format("%s : %s " + "has bowed back to me!%n",
this.name, bower.getName());
}
}

@SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws InterruptedException {
// wait for 4 seconds before send a interuption signal
int patient = 4000;
int waitingUnit = 1000;
long start;
final Freind alphonse = new Freind("Alphonse");
final Freind gaston = new Freind("Gaston");
// the first thread
Thread alphonseThread = new Thread(new Runnable() {
@Override
public void run() {
try {
alphonse.bow(gaston);
} catch (InterruptedException ex) {
System.out.println("Alphnso have been killed");
}
}
});
// the second thread
Thread gastonThread = new Thread(new Runnable() {
@Override
public void run() {
try {
gaston.bow(alphonse);
} catch (InterruptedException ex) {
System.out.println("Gaston have been killed ");
}
}
});
// start the tow threads
start = System.currentTimeMillis();
alphonseThread.start();
gastonThread.start();
while (alphonseThread.isAlive() || gastonThread.isAlive()) {
if (((System.currentTimeMillis() - start) < patient)
&& (alphonseThread.isAlive() || gastonThread.isAlive())) {
System.out.println(System.currentTimeMillis() - start);
System.out.println("still waiting !!");
Thread.sleep(waitingUnit);
} else {
if (alphonseThread.isAlive()) {
alphonseThread.interrupt();
alphonseThread.join();
}
if (gastonThread.isAlive()) {
gastonThread.interrupt();
gastonThread.join();
}
}
}
System.out.println("finnaly I kill all of them");
}
}

最佳答案

but why can't interrupt them from the main Thread?

您可以从主线程(或任何地方)中断。只需在要中断的线程上调用 Thread.interrupt()

问题是 interrupt 不会解除阻塞试图获取 Java 原始锁/互斥锁的线程。事实上,我认为没有任何东西可以安全地解除阻塞在原始​​锁上死锁的线程。

如果您希望您的应用程序能够从涉及对象锁的死锁中恢复,那么您可能应该使用Lock 对象,并且 Lock.lockInterruptibly()方法 ... 当线程被中断时,它将解除阻塞。

关于java 并发 - 中断 DeadLook 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11813766/

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