Java deadlock problems and solutions

1. What is a deadlock?

A thread waiting for a resource held by the resource b, b thread B waiting for a resource held by the resource to form a deadlock condition.

2. Case


public class Moive {
	Object o1 = new Object();//资源o1
	Object o2 = new Object();//资源o2
	boolean flag;
	public void buyAndWatch(String s) {
		if (flag) {
			synchronized (o1) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(s + "买票");
				synchronized (o2) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					System.out.println(s + "看电影");
				}
			}
		}else {
			synchronized (o2) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(s + "买票");
				synchronized (o1) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					System.out.println(s + "看电影");
				}
			}
		}
		
	}
public class MyRunnable implements Runnable {
	Moive moive = new Moive();
	@Override
	public void run() {
		moive.flag = true;
		moive.buyAndWatch(Thread.currentThread().getName());
		moive.flag = false;
		moive.buyAndWatch(Thread.currentThread().getName());
	}
}

test:

public class LockTest {
	public static void main(String[] args) {
		MyRunnable runnable = new MyRunnable();
		Thread t1 = new Thread(runnable,"线程A:");
		Thread t2 = new Thread(runnable,"线程B:");
		t1.start();
		t2.start();
	}
}

The result is deadlock:
Here Insert Picture Description

3. The four necessary conditions for deadlock and solutions

 资源互斥:资源不能被共享,只能由一个线程使用(两个人的不能穿同一件衣服)
 请求保持:拥有资源的线程在请求新的资源又不释放占有的资源(拿着别人需要的资源又去请求新的资源)
 不可抢占:已经获得的资源在使用完成前不被别人剥夺(自己的资源不放手)
 循环等待:各个线程对资源的需求构成一个循环(A线程需要B线程占用的资源,B线程需要C线程占用的资源,C线程需要A线程占用的资源)

Solution :

  1.调整申请锁的范围(不在锁上加锁)
  2.调整申请锁的顺序()
  3.人工检查:
	 在一个同步方法中,或在一个锁的保护的范围中,调用了其它对象的方法时,要注意:
 		a. 如果其它对象的方法会消耗比较长的时间,那么就会导致锁被我们持有了很长的时间; 
 		b. 如果其它对象的方法是一个同步方法,那么就要注意避免发生死锁的可能性了;
	  总之尽量避免在一个同步方法中调用其它对象的延时方法和同步方法。

When the deadlock four conditions occur simultaneously , a condition in which damage can break the deadlock state

Published 28 original articles · won praise 1 · views 524

Guess you like

Origin blog.csdn.net/qq_40575302/article/details/104608594