Java规避线程死锁:允许线程抢占资源

测试用储户实体类:

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 + "]";
	}

}

转账线程:

package domain;

/**
 * 转账线程(允许线程抢占资源)
 * 
 * @author WanAkiko
 * 
 */
public class AllowsPreemptThread implements Runnable {

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

	/**
	 * 转账线程全参构造
	 * 
	 * @param incomeObject
	 * @param spendingObject
	 * @param money
	 */
	public AllowsPreemptThread(Depositor incomeObject,
			Depositor spendingObject, Double money) {
		super();
		this.incomeObject = incomeObject;
		this.spendingObject = spendingObject;
		this.money = money;
	}

	@Override
	public void run() {
		Depositor depositor = new Depositor();
		while (true) {
			try {
				// 为未携带锁的对象扣上资源锁
				if (depositor.resourceLocked(incomeObject, spendingObject)) {
					// 收入对象的余额大于或等于目标金额时
					if (spendingObject.getBalance() >= money) {
						spendingObject.expenditure(money); // 资金支出
						incomeObject.earning(money); // 资金收入
						System.out.println("出账用户-" + spendingObject.getAccount() + 
								",剩余余额:" + spendingObject.getBalance() + 
								"¥;进账用户:" + incomeObject.getAccount() + 
								",当前余额:" + incomeObject.getBalance() + "¥");
					}
				}
			} finally {
				depositor.resourceRelease(incomeObject, spendingObject);
			}
		}
	}

}

死锁规避测试:

package test;

import domain.Depositor;
import domain.StopLoopWaitThread;

/**
 * 规避线程死锁(允许线程抢占资源)
 * 
 * @author WanAkiko
 *
 */
public class ResourcePreemptTest {

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

	}

}

运行示例:
Java规避线程死锁:允许线程抢占资源

上一篇:java继承与super练习


下一篇:nginx创建一个代理服务