本文主要是介绍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;}@Overridepublic String toString() {return "Depositor [account=" + account + ", balance=" + balance + "]";}}
转账线程:
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;}@Overridepublic 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();}}}}}
死锁规避测试:
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();}}
运行示例:
这篇关于Java规避线程死锁:打破单独持有,互相等待的僵局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!