java多线程存取钱(基于lock与unlock)

该程序创建了一个Account类,包含存款和取款方法,使用ReentrantLock和Condition进行同步。Husband线程负责存钱,Wife线程负责花钱,当余额超过10万或不足时,线程会进行等待。TestMoney类启动线程模拟这一过程。
摘要由CSDN通过智能技术生成

题目描述:
Wife线程类,负责花钱1万的随机整数倍,每次不超过10万,
如果余额不够但不为0,则有多少就花多少,没钱就wait;
Husband线程类,负责赚钱,2万的随机整数倍,每次不超过10万;
如果余额超过10万(含),则wait;如果余额加上新赚的钱超过10万,
则保留(例如余额5万,新赚6万,则新余额11万)
Account钱类(属性:int money,下限0万元)
TestMoney场景类,启动一个Husband线程,启动两个Wife线程,
模拟老公赚钱,老婆花钱的过程.
 

import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class practice05 {
    private static Account account = new Account();
    private static class Account{
        //账户余额
        private int balance;
        private Lock lock = new ReentrantLock(true);
        //存款
        private Condition deposit = lock.newCondition();
        public void setBalance(int balance){
            this.balance = balance;
        }

        //Husband线程类(存钱)
        public void Husband(int amount){
            try {
                lock.lock();
                while (balance + amount < 10){
                    balance += amount;
                    System.out.println("Husband存入金额:" + amount + " ,当前余额:" + balance);
                    //只要存入钱就尝试唤醒Wife(取钱)线程
                    deposit.signal();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();//释放锁
            }
        }


        public void Wife(int amount){
            try {
                lock.lock();
                if(amount > balance && balance > 0){
                    System.out.println("虽然想花:" + amount + "元,但存款只有:" + balance + ",花掉:" + balance);
                    balance -= balance;
                }else if(balance == 0){
                    System.out.println("余额不足,等待Husband存款");
                    deposit.await();
                }else{
                    balance -= amount;
                    System.out.println("Wife取款:" + amount + ",当前余额:" + balance);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }

    //Husband(存款)线程
    private static class HusbandTask implements Runnable{
        @Override
        public void run() {
            try {
                while(true){
                    account.Husband((int) (Math.random() * 5) * 2 + 2);
                    //线程休眠1秒
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //Wife(取款)任务线程
    private static class WifeTask implements Runnable{
        @Override
        public void run() {
            try {
                while (true){
                    account.Wife((int) (Math.random() * 10) + 1);
                    //线程休眠1秒
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //主函数调用执行线程
    public static void main(String[] args) {
        var executor = Executors.newFixedThreadPool(3);
        //一个Husband线程存钱
        executor.execute(new HusbandTask());
        //两个Wife线程花钱
        executor.execute(new WifeTask());
        executor.execute(new WifeTask());
        //线程关闭
        executor.shutdown();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Uddddr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值