线程死锁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();

    }


}

 

posted @ 2018-08-29 16:31  小阿Q的博客  阅读(252)  评论(0编辑  收藏  举报