多线程顺序的控制(wait,notity,sleep)

public class abc extends Thread{
    private Object prev=null;
    private Object self=null;
    private String msg=null;
    public abc(Object prev,Object self,String msg){
        this.prev=prev;
        this.self=self;
        this.msg=msg;
    }
    public void run(){
        int count=0;
        while(count<1000){
            synchronized(prev){
                synchronized(self){
                    System.out.print(msg);
                    if(msg.contains("C"))
                        System.out.println();
                    count++;
                    self.notify();
                }
                if(count==1000)
                break;
                try{
                    prev.wait();}
                catch(Exception e)
                    {e.getMessage();}
            }
            
        }
        
    }
    public static void main(String[] args){
        Object a=new Object();
        Object b=new Object();
        Object c=new Object();
        abc abc1=new abc(c,a,"A");
        abc abc2=new abc(a,b,"B");
        abc abc3=new abc(b,c,"C");
        try{
        abc1.start();Thread.sleep(100);}catch(Exception e){e.getMessage();}
        try{
        abc2.start();Thread.sleep(100);}catch(Exception e){e.getMessage();}
        abc3.start();
    }
}

原文地址:https://www.cnblogs.com/heysong/p/3733477.html