社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
bug
死锁发生场景之一大概就是。
如果有两个线程A and B。
如果A执行了一段任务想等B执行完成再执行,同样如果B如果执行了一半想等等A。那么就会一直等到天长地久QWQ。
实例代码:
public static void main(String[] args) {
test2();
}
public static void test2(){
class A{
Thread threadA = null;
Thread threadB = null;
public void doSome(){
threadA = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("A start");
try {
threadB.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A end");
}
});
threadA = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("B start");
try {
threadA.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("B end");
}
});
threadA.start();
threadB.start();
}
};
new A().doSome();
}
你会发现运行了Start 便没有了end。那么我们成功的写了一个死锁bug。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!