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();
    
        }
    
    
    }
  • 相关阅读:
    【转】QT创建子对话框的方法
    IplImage转为Mat的方法
    浅谈Android选项卡(二)
    浅谈Android选项卡(一)
    Android来电、去电监听
    文件加密
    Java实现文件重命名
    使用单个httpclient实例请求数据。
    获取Android状态栏的高度
    [置顶] 微软翻译接口
  • 原文地址:https://www.cnblogs.com/gcm688/p/9555212.html
Copyright © 2011-2022 走看看