• 死锁演示


    package leo.wan.test.thread;
    
    public class TestDeath {
        public static void main(String[] args) {
            DeathWorker worker = new DeathWorker();
            new Thread(){
                @Override
                public void run() {
                    try {
                        worker.lock2WantLock1();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            new Thread(){
                @Override
                public void run() {
                    try {
                        worker.lock1WantLock2();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
    package leo.wan.test.thread;
    
    public class DeathWorker {
        private final Object lock1 = new Object();
        private final Object lock2 = new Object();
        public void lock1WantLock2() throws InterruptedException {
            synchronized (lock1){
                Thread.sleep(10000L);
                System.out.println(Thread.currentThread().getName()+"占领了lock1,等待lock2");
                lock2WantLock1();
            }
        }
        public void lock2WantLock1() throws InterruptedException {
            synchronized (lock2){
                Thread.sleep(10000L);
                System.out.println(Thread.currentThread().getName()+"占领了lock2等待lock1");
                lock1WantLock2();
            }
        }
    }
  • 相关阅读:
    Windows中的库编程(三、函数调用约定 Calling Convention)
    weui
    js 压缩图片
    django 跨域访问
    html5
    有用的网站
    Chrome
    srpingBoot配置多环境配置文件
    Mysql在查询时不区分大小写
    [CentOS7]Nginx 1.20.1不支持四层负载
  • 原文地址:https://www.cnblogs.com/wanjn/p/12708608.html
Copyright © 2020-2023  润新知