zoukankan      html  css  js  c++  java
  • 线程死锁demo

    package example.xcdemo;
    
    /**
     * @author MM
     * @create 2018-08-10 11:06
     **/
    public class DeadLockDemo {
    
        private static String resource_a = "A";
        private static String resource_b = "B";
    
        public static void main(String[] args) {
            deadLock();
        }
    
        private static void deadLock() {
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resource_a) {
                        System.out.println(Thread.currentThread().getName() + " get resource a: " + System.currentTimeMillis());
                        try {
                            Thread.sleep(10);
                            synchronized (resource_b) {
                                System.out.println(Thread.currentThread().getName() + " get resource b: " + System.currentTimeMillis());
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
    
            Thread threadB = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resource_b) {
                        System.out.println(Thread.currentThread().getName() + " get resource a: " + +System.currentTimeMillis());
                        try {
                            Thread.sleep(10);
                            synchronized (resource_a) {
                                System.out.println(Thread.currentThread().getName() + " get resource b: " + System.currentTimeMillis());
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
    
            threadA.start();
            threadB.start();
    
        }
    
    
    }
  • 相关阅读:
    高并发的优化策略
    Linux Ctrl+c与ctrl+z的区别
    数据库连接池的工作原理
    JDBC数据库连接池原理
    JSP页面批量选择&全选操作&选择回显
    通过火狐谋智查询API
    通过console.log()打印window对象的属性、方法、事件
    制作 首页
    JavaScript 函数参数
    JavaScript 函数
  • 原文地址:https://www.cnblogs.com/gcm688/p/9555212.html
Copyright © 2011-2022 走看看