java多线程练习-线程通信-取钱案例

java多线程练习-线程通信-取钱案例

案例需求:
      小明打算去提款机上取钱,发现卡上没有钱,这时候他告知妈妈去存钱,妈妈存了钱了,告知小明存好了可以取钱了。小明分多次取钱,每次取100,当发现钱不够100,就等妈妈存钱妈妈每次存钱2000,当发现钱小于100就存钱,并且通知小明取取钱,当大于100就等小明钱不够再存

public class XiaoMing {

    private static Byte[] bytes = new Byte[0];

    public static void main(String[] args) {
        // 取得线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (bytes) {
                    while (true) {
                        Integer money = Mother.getMoney();
                        System.out.println("小明取钱一次,余额:" + (money)+"元");                        
                        if (money==0) {
                            try {
                                System.out.println("钱不足,剩余:"+money);
                                bytes.notifyAll();
                                System.out.println("等妈妈放钱");
                                Thread.sleep(5000);
                                bytes.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();
        // 放的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (bytes) {
                    while (true) {
                        Mother.add();
                        System.out.println("妈妈放钱成功");
                        bytes.notifyAll();
                        try {
                            bytes.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    }
                }
        }).start();
    }
}
 class Mother {
    private static int count = 0;

    static {
        add();
    }

    public static Integer getMoney() {
        return count-=100;
    }

    public static void add() {
        count += 2000;
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是关于Java线程的存问题。这是一个经典的多线程同步问题,可以通过使用synchronized关键字来实现线程安全。下面是一个简单的示例代码: ```java public class BankAccount { private int balance; public BankAccount(int balance) { this.balance = balance; } public synchronized void deposit(int amount) { balance += amount; System.out.println("Deposit successful. Balance: " + balance); } public synchronized void withdraw(int amount) { if (balance < amount) { System.out.println("Withdraw failed. Insufficient balance."); return; } balance -= amount; System.out.println("Withdraw successful. Balance: " + balance); } } public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000); // Create two threads to simulate deposit and withdraw Thread depositThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.deposit(100); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread withdrawThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.withdraw(200); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); depositThread.start(); withdrawThread.start(); } } ``` 在这个示例代码中,我们创建了一个银行账户类BankAccount,并在其中实现了deposit和withdraw方法,并使用synchronized关键字来保证线程安全。 在main方法中,我们创建了两个线程来模拟存款和款操作,每个线程执行5次操作。我们使用Thread.sleep方法来模拟每个操作之间的间隔,以便更好地观察多线程操作的结果。 当多个线程同时访问BankAccount对象的deposit和withdraw方法时,synchronized关键字可以确保每个方法只能被一个线程访问,从而避免了竞争条件和数据不一致的问题。 希望这个示例代码能够回答您的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值