
死锁是指由于执行过程中对资源的竞争而导致两个或多个线程互相等待的现象. 没有外力,这些线程将彼此等待,并且无法继续运行Go.



package com.zte.test.factory;
class DeadLockTest2 {
private static Object resourceA = new Object();
private static Object resourceB = new Object();
public static void main(String[] args) {
Thread threadA = new Thread(new Runnable() {
public void run() {
synchronized (resourceA) {
System.out.println(Thread.currentThread() + "get ResourceA");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get ResourceB");
synchronized (resourceB) {
System.out.println(Thread.currentThread() + "get ResourceB");
}
}
}
});
Thread threadB = new Thread(new Runnable() {
public void run() {
synchronized (resourceB) {
System.out.println(Thread.currentThread() + "get ResourceB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get ResourceA");
synchronized (resourceA) {
System.out.println(Thread.currentThread() + "get ResourceA");
}
}
}
});
threadA.start();
threadB.start();
}
}

结果显示:


为避免死锁,您只需要销毁至少一个构造死锁的必要条件java多线程死锁java多线程死锁,但是了解操作系统的读者应该知道,当前只有请求,保持和循环等待条件可以被破坏.
实际上,死锁的原因与资源应用程序的顺序有很大关系. 使用资源应用的有序原则可以避免死锁.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-158708-1.html
对股票说三道四
第四