線程:a,b
資源:1,2
簡單說就是線程a持有資源1需要資源2,線程b持有資源2需要資源1,產(chǎn)生死鎖。
public class DeadLock {
public static void main(String[] args) {
ReentrantLock alock = new ReentrantLock();
ReentrantLock block = new ReentrantLock();
new Thread(()->{
alock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
block.lock();
block.unlock();
alock.lock();
}).start();
new Thread(()->{
block.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
alock.lock();
alock.unlock();
block.lock();
}).start();
}
}