线程死锁案例

本文深入探讨了线程死锁的概念,通过实例解释了死锁如何发生,并提供了预防死锁的关键策略。读者将了解到Java中锁的使用以及如何通过合理设计同步代码来避免死锁问题。
第一种:synchronized
package gstation.zerotech.javademo;

import sun.rmi.runtime.Log;

/**
 * 线程的状态 new 就绪 running wait(阻塞) terminal(结束)
 * Created by admin on 2018/5/18.
 */

public class SyncDeadLock {
    private static Object locka = new Object();
    private static Object lockb = new Object();

    public void deadLock(){
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (locka){
                    try {
                        System.out.println(Thread.currentThread().getName() + " get locka ing!");
                        //进入休眠状态,但是不释放锁
                        Thread.sleep(500);
                        System.out.println(Thread.currentThread().getName() + " after sleep 500ms!");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        //进入等待,并且释放当前锁
                        locka.wait();//释放锁,进入无线等待,防止死锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " need lockb! Just waiting!");
                    synchronized (lockb){
                        System.out.println(Thread.currentThread().getName() + "get lockb ing!");
                    }
                }
            }
        },"thread1");

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockb){
                    try {
                        System.out.println(Thread.currentThread().getName() + " get lockb ing!");
                        Thread.sleep(500);
                        System.out.println(Thread.currentThread().getName() + " after sleep 500s!");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " need locka! Just waiting!");
                    synchronized (locka){
                        System.out.println(Thread.currentThread().getName() + " get locka ing!");
                        locka.notify();//执行完唤醒locka的等待者
                    }
                }
            }
        },"thread2");

        thread1.start();
        thread2.start();
    }
}
第二种: Lock

package gstation.zerotech.javademo;

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

/**
 * Created by admin on 2018/5/18.
 */

public class LockDeadDemo {

    public void test(){
        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 will join");
            threadA.join();
            threadB.join();
            System.out.println("main recover from join");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }



    class DeadLockBean{
        private Lock lock = new ReentrantLock();
        public void productDeadLock() throws Throwable {
            System.out.println(Thread.currentThread().getName() + " in method");
            lock.lock();

            try {
                System.out.println(Thread.currentThread().getName() + " excute method");
//                throw new Throwable("paochu Throwable!");
                throw new Exception("paochu yichang");
            } catch (Exception e) {
                System.out.println(Thread.currentThread().getName() + " shasheng catch!");
//                lock.unlock();
            }finally {
                System.out.println(Thread.currentThread().getName() + " fasheng finally!");
                lock.unlock();//无论发生任何异常都会释放锁
            }
            System.out.println(Thread.currentThread().getName() + "  trycatch shifang lock");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值