线程死锁与解决

本文探讨了线程死锁的产生条件,包括互斥、占有且等待、不可抢占和循环等待。为了防止死锁,可以采取重启应用或者破坏死锁条件,如避免占有且等待的情况。文中通过实例说明了如何破坏占有且等待条件来防止死锁。
摘要由CSDN通过智能技术生成

产生死锁的条件

  • 互斥,共享资源A和共享资源B只能被一个线程占用
  • 占有且等待,线程1已经取得共享资源A,在等待共享资源B的时候不释放共享资源A
  • 不可抢占,其他线程不能强行抢占线程1占有的资源
  • 循环等待,线程1等待线程2占有的资源,线程2等待线程1占有的资源,就是循环等待

如何解决死锁

  • 重启应用
  • 提前规避(破坏死锁条件的一个,互斥是锁的本身一个条件,无法破坏)

死锁举例

/**
 * 创建一个账号类
 */
public class Account {
    private String accountName;
    private int price;

    public Account(String accountName, int price) {
        this.accountName = accountName;
        this.price = price;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    /*
    转账账号减钱
     */
    public void delete(int money){
        this.price -= money;
    }

    /*
   收款账号加钱
    */
    public void add(int money){
        this.price += money;
    }

}
/**
 * 实现一个多线程
 */
public class TransferAccount implements Runnable{
    private Account fromAccount;
    private Account toAccount;
    private int amount;

    public TransferAccount(Account fromAccount, Account toAccount, int amount) {
        this.fromAccount = fromAccount;
        this.toAccount = toAccount;
        this.amount = amount;
    }

    @Override
    public void run() {
        while(true){
            synchronized (fromAccount){
                synchronized (toAccount){
                    if(fromAccount.getPrice() >= amount){
                        fromAccount.delete(amount);
                        toAccount.add(amount);
                        System.out.println(fromAccount.getAccountName()+"------"+fromAccount.getPrice());
                        System.out.println(toAccount.getAccountName()+"------"+toAccount.getPrice());
                    }
                }
            }
        }
    }
}
/**
 * 测试死锁
 */
public class Test {
    public static void main(String[] args) {
        Account from = new Account("张三",100);
        Account to = new Account("李四",50);

        Thread a = new Thread(new TransferAccount(from,to,1));
        Thread b = new Thread(new TransferAccount(to,from,3));

        a.start();
        b.start();
    }
}

破坏死锁例子(占有且等待条件)

/**
 * 创建一个管理角色防止占有且等待
 */
public class Allcator {
    private List<Object> list  = new ArrayList<>();

    /**
     * 申请锁资源
     * @param from
     * @param to
     * @return
     */
    synchronized boolean apply(Object from,Object to){
        if(list.contains(from) || list.contains(to)){
            return false;
        } else {
            list.add(from);
            list.add(to);
            return true;
        }
    }

    /**
     * 释放锁资源
     * @param from
     * @param to
     */
    synchronized void free(Object from,Object to){
        list.remove(from);
        list.remove(to);
    }
}
public class TransferAccount implements Runnable{
    private Account fromAccount;
    private Account toAccount;
    private int amount;
    Allcator allcator;
    public TransferAccount(Account fromAccount, Account toAccount, int amount,Allcator allcator) {
        this.fromAccount = fromAccount;
        this.toAccount = toAccount;
        this.amount = amount;
        this.allcator= allcator;
    }

    @Override
    public void run() {
        while(true){
            if(allcator.apply(fromAccount,toAccount)){//在同一个临界资源
                try {
                    synchronized (fromAccount) {
                        synchronized (toAccount) {
                            if (fromAccount.getPrice() >= amount) {
                                fromAccount.delete(amount);
                                toAccount.add(amount);
                                System.out.println(fromAccount.getAccountName() + "------" + fromAccount.getPrice());
                                System.out.println(toAccount.getAccountName() + "------" + toAccount.getPrice());
                            }
                        }
                    }
                } finally {
                    allcator.free(fromAccount,toAccount);
                }
            }
        }
    }
}
/**
 * 测试破坏死锁
 */
public class Test {
    public static void main(String[] args) {
        Account from = new Account("张三",100);
        Account to = new Account("李四",50);
        Allcator allcator = new Allcator();//统一分配锁

        Thread a = new Thread(new TransferAccount(from,to,1,allcator));
        Thread b = new Thread(new TransferAccount(to,from,3,allcator));

        a.start();
        b.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值