当然,第二个循环永远不会被执行,但仍然想知道以下示例是否可以归类为死锁。
public class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 1; i <= 10; i++)
System.out.print(i + " ");
Thread.currentThread().join();
for (int i = 11; i <= 20; i++)
System.out.print(i + " ");
}
}
最佳答案
不,这不是僵局。但你确实无限期地阻塞了你的主线程。 join
method阻塞直到调用它的Thread
死亡。
Waits for this thread to die.
调用Thread.currentThread().join();
意味着主线程正在等待自身死亡,而这不会发生。
这与死锁不同,死锁中两个线程各自拥有不同资源上的锁,但每个线程都需要对方锁定的资源才能继续。这里的情况并非如此。
关于java - 这是代码中的一种死锁吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142166/