死锁java_java两种经典死锁例子,Lock发生死锁案列

package com.app.test;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

* Created by lirong5 on 2016/5/24.

*/

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外释放锁!");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值