public class BankExample {
private int money;
public BankExample(int money){
this.money =money;
}
public synchronized void takeOut(String usr,int moneyOut){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
money = money - moneyOut;
System.out.println(usr+“取钱”+moneyOut+",银行结余:"+money);
}
}
public class Test {
public static void main (String[] args){
BankExample bank = new BankExample(3000);
for(int i = 0;i<3;i++) {
String usr = “用户”+i;
new Thread(new Runnable(){//内部类
public void run(){
bank.takeOut(usr, 1000);
}
}).start();
}
}
}
03-07
172
06-15
06-15
06-15
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交