我调试这个代码,我不明白为什么我会陷入僵局.当您执行此代码时,它看起来像是连接方法中的主线程锁,而另一个线程正在等待获取锁.
public class Foo {
private final Thread thread;
public Foo() {
thread = new Thread(new Bar(), "F");
thread.start();
}
public void run() {
synchronized (this) {
thread.interrupt();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Foo run method");
}
}
private final class Bar implements Runnable {
@Override
public void run() {
synchronized (Foo.this) {
System.out.println("Bar run method");
}
}
}
public static void main(String[] args) throws InterruptedException {
final Foo foo = new Foo();
foo.run();
} …
Run Code Online (Sandbox Code Playgroud)