在java中如何避免死锁,多线程开发时,经常会遇到这样的问题,什么是死锁,简单的说就是两个或多个线程,等待对方释放某个资源而卡死的情况称之为死锁
1.在Java代码中如何检查是否有死锁?
①在不借助其它工具的话,只能从代码中synchronized block和调用synchronized 方法的地方着手检查代码 ②如果程序已经出现了死锁,可以通过获取Thread的dump来分析,在linux上可以执行"kill -3 pid",的方式发送一个SIGQUIT信号给Java应用之后,通常会有当前的Thread Dump输出,从Thread 的dump log可以看出那个Theread lock了那个object。③另外一种方式是通过jconsole,可以清楚的看出那些Thread locked的2.死锁的实例
public class DeadLockDemo { /* * * This method request two locks, first String and then Integer */ public void method1() { synchronized (String.class) { System.out.PRintln("Aquired lock on String.class object"); synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); } } } /* * * This method also requests same two lock but in exactly Opposite order * i.e. first Integer and then String. This creates potential deadlock, if * one thread holds String lock and other holds Integer lock and they wait * for each other, forever. */ public void method2() { synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); synchronized (String.class) { System.out.println("Aquired lock on String.class object"); } } }}在上面的代码中,如果method1和method2同时被多个线程调用,就很容易出现死锁。线程A在执行method1()时lock了String对象,线程B在执行method2()时lock了Integer对象,相互都在等待对方释放锁,从而出现了死锁,具体可以看下图
3.怎么避免死锁在java中?面试时经常会被遇到如何避免死锁,如果你仔细看上面的代码,你可能发现死锁的真正原因不是多线程但他们请求锁的方式。如果你提供一个有序的访问,那么问题将得到解决public class DeadLockFixed { /** * * Both method are now requesting lock in same order, first Integer and * then String. * You could have also done reverse e.g. first String and * then Integer, * both will solve the problem, as long as both method are * requesting lock * in consistent order. */ public void method1() { synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); synchronized (String.class) { System.out.println("Aquired lock on String.class object"); } } } public void method2() { synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); synchronized (String.class) { System.out.println("Aquired lock on String.class object"); } } }}现在应该不会出现死锁了,因为method1和method2都是先lock Integer,然后lock String,当ThreadA调用method1 lock了Integer后ThreadB调用method2后,只有当ThreadA释放了Integer Object后method2中方法才会执行,从而有效的避免了死锁。
新闻热点
疑难解答