08.死锁

/**
 * 线程死锁
 * 是指两个或两个以上的线程在执行过程中,因争夺资源而造成的互相等待的现象,
 * 在无外力作用的情况下,这些线程会一直相互等待而无法继续运行下去
 */
public class DeadLockDemo {
    private static Object reA = new Object();
    private static Object reB = new Object();
    public static void main(String[] args) throws InterruptedException {
        final Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (reA){
                    System.out.println(Thread.currentThread()+"get reA");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+" wait get reB");
                    synchronized (reB){
                        System.out.println(Thread.currentThread()+" get reB");
                    }
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (reB){
                    System.out.println(Thread.currentThread()+" get reB");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+" wait get reA");
                    synchronized (reA){
                        System.out.println(Thread.currentThread()+" get reA");
                    }
                }
            }
        });
        threadA.start();
        threadB.start();
        Thread.sleep(2000);
        //Thread[Thread-0,5,main]get reA
        //Thread[Thread-1,5,main] get reB
        //Thread[Thread-0,5,main] wait get reB
        //Thread[Thread-1,5,main] wait get reA
    }
}

demo2


/**
 * 死锁问题
 */
public class ThreadTrain1 implements Runnable {
    private int count = 100;
    public boolean flag = true;
    private static Object o = new Object();
    @Override
    public void run() {
        if (flag){
            while (count>0){
                synchronized (o){
                    // 如果flag为true 先拿到 o锁,在拿到this 锁、 才能执行。
                    // 如果flag为false先拿到this,在拿到o锁,才能执行。
                    sale();
                }
            }
        }else {
            while (count>0){
                sale();
            }
        }
    }

    private synchronized void sale() {
        // 前提 多线程进行使用、多个线程只能拿到一把锁。
        // 保证只能让一个线程 在执行 缺点效率降低
        synchronized (o){
            if (count>0){
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+",出售第.."+(100-count+1)+"张票");
                count--;
            }
        }
    }
    public static void main(String[] args) throws InterruptedException{
        ThreadTrain1 train1 = new ThreadTrain1();
        Thread t1 = new Thread(train1,"1号窗口");
        Thread t2 = new Thread(train1,"2号窗口");
        t1.start();
        Thread.sleep(40);
        train1.flag = false;
        t2.start();
    }
}
原文地址:https://www.cnblogs.com/fly-book/p/11363864.html