| 
 
| ** * 模拟两个人向银行存钱,每次存500,存三次
 */
 public class Test01 {
 public static void main(String[] args) {
 Bank b=new Bank();
 Thread s1=new Thread(b,"01号");
 Thread s2=new Thread(b,"02号");
 s1.start();
 s2.start();
 }
 }
 class Bank implements Runnable {
 private int money=0;//银行的钱
 private int num=0;//次数
 public void run() {
 while(true){
 synchronized (this) {
 if (num>=3){
 break;
 }
 System.out.println(Thread.currentThread().getName()+"向银行存入500元");
 money=money+500;
 num++;
 System.out.println("账户余额"+money);
 }
 }
 }
 }
 
 | 
 |