package chapter03; import com.sun.source.tree.SynchronizedTree; public class Test03 { public static void main(String[] args) { Num num = new Num(); User user = new User(num); user.start(); Bank bank = new Bank(num); bank.start(); } } class Num{ } class Bank extends Thread{ private Num num; public Bank(Num num) { this.num = num; } public void run(){ synchronized (num) { try{ Thread.sleep(2000); } catch(InterruptedException e) { throw new RuntimeException(e); } System.out.println("9:00,银行开门"); num.notifyAll(); } } } class User extends Thread{ private Num num; public User(Num num) { this.num = num; } public void run() { synchronized ( num){ System.out.println("我是1,银行还没有开门"); try { num.wait(); } catch(InterruptedException e) { throw new RuntimeException(e); } System.out.println("叫到我的号了,我该办业务了"); } } }
打印: