死锁Waiting--DeadLockDemo
生活随笔
收集整理的這篇文章主要介紹了
死锁Waiting--DeadLockDemo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.concurrent.locks.ReentrantLock;/*** 死鎖演示.<br>* @author gongqiang <br>* @version 1.0.0 2021年6月4日<br>* @see * @since JDK 1.5.0*/
public class DeadLockDemo {/*** @param args*/public static void main(String[] args) {ReentrantLock lockA = new ReentrantLock();ReentrantLock lockB = new ReentrantLock();new Thread(() -> {while (true) {try {lockA.lock();System.out.println(Thread.currentThread().getName() + "成功獲取鎖A,等待3秒獲取鎖B...");Thread.sleep(3 * 1000);lockB.lock();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();new Thread(() -> {while (true) {try {lockB.lock();System.out.println(Thread.currentThread().getName() + "成功獲取鎖B,等待3秒獲取鎖A...");Thread.sleep(3 * 1000);lockA.lock();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();try {Thread.sleep(60 * 60 * 1000);} catch (InterruptedException e) {// 無需處理.}}}
?
總結
以上是生活随笔為你收集整理的死锁Waiting--DeadLockDemo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生产者-消费者 BlockingQueu
- 下一篇: Replace Exception wi