Java circumvents thread deadlock: breaking the deadlock of holding alone and waiting for each other

Test depositor entity class:

package domain;

import java.util.ArrayList;

/**
 * 规避死锁问题的共享存取实体类
 * 
 * @author WanAkiko
 * 
 */
public class Depositor {

	private String account;
	private Double balance;
	private ArrayList<Depositor> list = new ArrayList<Depositor>();

	public Depositor() {
		super();
	}

	public Depositor(String account, Double balance) {
		super();
		this.account = account;
		this.balance = balance;
	}

	/**
	 * 资金收入
	 */
	public void earning(Double money) {
		this.balance += money;
	}

	/**
	 * 资金支出
	 */
	public void expenditure(Double money) {
		this.balance -= money;
	}

	/**
	 * 资源上锁
	 * 
	 * @param depThis
	 * @param depThat
	 * @return
	 */
	public synchronized boolean resourceLocked(Depositor depThis,
			Depositor depThat) {
		// 若对象序列中存在任意携锁对象则无需继续上锁
		if (list.contains(depThis) || list.contains(depThat) || list.size() > 0) {
			return false;
		}
		list.add(depThis);
		list.add(depThat);
		return true;
	}

	/**
	 * 资源释放
	 * 
	 * @param depThis
	 * @param depThat
	 */
	public synchronized void resourceRelease(Depositor depThis,
			Depositor depThat) {
		list.remove(depThis);
		list.remove(depThat);
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Double getBalance() {
		return balance;
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return "Depositor [account=" + account + ", balance=" + balance + "]";
	}

}

Transfer thread:

package domain;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 转账线程(打破单独持有,互相等待的僵局)
 * 
 * @author WanAkiko
 * 
 */
public class WaitEachOtherThread implements Runnable {

	private Depositor incomeObject; // 收入对象
	private Depositor spendingObject; // 支出对象
	private Double money = 0.0; // 交易金额
	private ReentrantLock iLock; // 收入锁
	private ReentrantLock sLock; // 支出锁

	/**
	 * 转账线程全参构造
	 * 
	 * @param 收入账户
	 * @param 支出账户
	 * @param 单位金额
	 * @param 收入锁
	 * @param 支出锁
	 */
	public WaitEachOtherThread(Depositor incomeObject,
			Depositor spendingObject, Double money, ReentrantLock iLock,
			ReentrantLock sLock) {
		super();
		this.incomeObject = incomeObject;
		this.spendingObject = spendingObject;
		this.money = money;
		this.iLock = iLock;
		this.sLock = sLock;
	}

	@Override
	public void run() {
		while (true) {
			// 仅当锁对象未被其它线程使用时才获取该锁
			if (iLock.tryLock() && sLock.tryLock()) {
				try {
					// 收入对象的余额大于或等于目标金额时
					if (spendingObject.getBalance() >= money) {
						spendingObject.expenditure(money); // 资金支出
						incomeObject.earning(money); // 资金收入
						System.out.println("出账用户-"
								+ spendingObject.getAccount() + ",剩余余额:"
								+ spendingObject.getBalance() + "¥;进账用户-"
								+ incomeObject.getAccount() + ",当前余额:"
								+ incomeObject.getBalance() + "¥");
					}
				} finally {
					// 循环释放线程锁,预防“超出最大锁数量”的错误(Maximum lock count exceeded)
					iLock.unlock();
					sLock.unlock();
				}
			}
		}
	}

}

Deadlock avoidance test:

package test;

import java.util.concurrent.locks.ReentrantLock;

import domain.Depositor;
import domain.WaitEachOtherThread;

/**
 * 规避线程死锁(打破单独持有,互相等待的僵局)
 * 
 * @author WanAkiko
 *
 */
public class BreakWaitTest {

	public static void main(String[] args) {
		
		Depositor incomeObject = new Depositor("WanAkiko", 1000.0);
		Depositor spendingObject = new Depositor("WanQuee", 2000.0);
		ReentrantLock iLock = new ReentrantLock();
		ReentrantLock sLock = new ReentrantLock();
		new Thread(new WaitEachOtherThread(incomeObject, spendingObject, 2.0, iLock, sLock)).start();
		new Thread(new WaitEachOtherThread(incomeObject, spendingObject, 5.0, iLock, sLock)).start();

	}

}

Run example:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44965393/article/details/112729196