两种死锁例子

synchronized 方式

package com.usercard.open.rest;

/**
 * 段
 * Created by Heyiyong on 2018/8/30.
 * 死锁实例
 */
public class DeadLock implements Runnable {
    public int flag = 1;
    //静态对象是类的所有对象共享的
    private static Object o1 = new Object(), o2 = new Object();
    @Override
    public void run() {
        System.out.println("flag=" + flag);
        if (flag == 1) {
            synchronized (o1) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (o2) {
                    System.out.println("1");
                }
            }
        }
        if (flag == 0) {
            synchronized (o2) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (o1) {
                    System.out.println("0");
                }
            }
        }
    }

    public static void main(String[] args) {

        DeadLock td1 = new DeadLock();
        DeadLock td2 = new DeadLock();
        td1.flag = 1;
        td2.flag = 0;
        //td1,td2都处于可执行状态,但JVM线程调度先执行哪个线程是不确定的。
        //td2的run()可能在td1的run()之前运行
        new Thread(td1).start();
        new Thread(td2).start();

    }
}

 

LOCK死锁方式

package com.usercard.open.rest;

/**
 * 段
 * Created by Heyiyong on 2018/8/30.
 lock.unlock();释放锁使用地方不规范,导致死锁不能正常释放!
 不释放就死锁
 */

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDeadDemo {

    public static void main(String[] args){
        final DeadLockBean deadLockBean = new DeadLockBean();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    deadLockBean.productDeadLock();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        },"threadA");
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(310);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    deadLockBean.productDeadLock();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        },"threadB");
        threadA.start();
        threadB.start();
        try {
            System.out.println("main线程即将被join");
            threadA.join();
            threadB.join();
            System.out.println("main线程从join中恢复");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class DeadLockBean{
        private Lock lock = new ReentrantLock();
        public void productDeadLock() throws Throwable {
            System.out.println(Thread.currentThread().getName() + "   进入了方法!");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "   已经执行了!");
                throw new Throwable("人为抛出异常Throwable");//关键代码行1,
                //throw new Exception("人为抛出异常Exception");//关键代码行2,不会死锁,会在catch(Exception e中被捕获),嵌套lock.unlock()并释放
            }catch(Exception e){
                System.out.println(Thread.currentThread().getName()+"   发生异常catch!");
                //lock.unlock();//关键代码行3,不建议在这里释放,假如发生【关键代码行1】会产生死锁
            }finally{
                System.out.println(Thread.currentThread().getName()+"   发生异常finally!");
                lock.unlock();//关键代码行4,无论发生何种异常,均会释放锁。
            }
            //lock.unlock();//关键代码行5,假如发生不能捕获异常,将跳出方法体,不执行此处
            System.out.println(Thread.currentThread().getName() + "   tryCatch外释放锁!");
        }
    }
}

 

你可能感兴趣的:(两种死锁例子)