package step1;
public class Task {
    public static void main(String[] args) throws Exception {
        /********* Begin *********/
        //在这里创建线程, 开启线程
        Object A = new Object();
        Object B = new Object();
        Object C = new Object();
        MyThread thread1 = new MyThread("AA",C,A);
        MyThread thread2 = new MyThread("BB",A,B);
        MyThread thread3 = new MyThread("CC",B,C);
        thread1.start();
        thread1.sleep(100);
        thread2.start();
        thread2.sleep(100);
        thread3.start();
        /********* End *********/
    }
}
class MyThread extends Thread {
    /********* Begin *********/
    String threadName;
    private Object pre;
    private Object self;
    public MyThread(String threadName,Object pre,Object self){
        this.threadName = threadName;
        this.pre = pre;
        this.self = self;
    }
    public void run() {
        int count = 5;
        while(count > 0){
            synchronized(pre){
                synchronized(self){
                    System.out.println("Java Thread" + this.threadName);
                    count--;
                    self.notify();
                }
                try{
                    pre.wait();
                }catch(Exception e){
                }
            }        
        }
        System.exit(0);
    }
    /********* End *********/
}
package step2;
/********* Begin *********/
//定义站台类,实现卖票的功能。
public class Station  extends Thread  {     
    static int tick = 20; // 为了保持票数的一致,票数要静态
    static Object ob = new Object(); // 创建一个静态钥匙 值是任意的
    public void ticket() {
        System.out.println( "卖出了第" + tick + "张票");
        tick--;
    }
    public void run() {
        while (tick > 0) {
            synchronized (ob) {
                if (tick > 0) {
                    ticket();
                } 
            }
            if(tick == 0){
                System.out.println("票卖完了");
            }
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
        }
    }
}
/********* End *********/