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();
    
        }
    
    
    }
  • 相关阅读:
    Ansible中文权威指南学习
    gitlab
    Python-Day01-task
    Python-Day01
    CentOs6.7 python2.6升级到2.7.11
    网站访问慢排查方法及解决方案
    LAMP环境下zabbix安装配置
    监控知识点概述
    Java抽象类、接口和内部类
    Java 访问控制
  • 原文地址:https://www.cnblogs.com/gcm688/p/9555212.html
Copyright © 2011-2022 走看看