看了评论大佬们后 自己尝试了一下 public class MyTest { public static void main(String[] args) { Bank bank = new Bank(); bank.setMoney(0); bank.setFlag(false); new Thread(()->{ for (int i = 0; i < 5; i++) { bank.get_(100); } }).start(); new Thread(()->{ for (int i = 0; i < 10; i++) { bank.save_(50); } }).start(); } }
笔试估计凉了,一直想弄出这个题,把后面的题漏了,笔试后写出来了,取钱存钱都用的随机数,应该很好的满足了题意 //启动类 public class Main { public static void main(String[] args) { Message message=new Message(); new Getmoney(message).start(); new SetMoney(message).start(); } } //资源, public class Message { public volatile int money=0; //银行卡里存的钱 private boolean flag; //true代表已经存钱,false代表没存钱,等待存钱 public void setMoney(int money) { //计算钱总和 this.money += money; } public void setMoney2(int money) { this.money -= money; } public int getMoney() { return this.money; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } }