线程:a,b
资源:1,2
简单说就是线程a持有资源1需要资源2,线程b持有资源2需要资源1,产生死锁。
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();
}
}
网友评论