天天看点

Lock&Condition实现三个线程之间的通信

public class ConditionThreeCommunicationTest

{

    public static void main(String[] args)

    {

        final Business2 bu=new Business2();

        new Thread(){

            @Override

            public void run()

            {

                //synchronized(TraditionalThreadWaitAndNotify.class){

                    for(int i=0;i<50;i++){

                        bu.sub2(i);

                    }

                //}

            }

        }.start();

        new Thread(){

            @Override

            public void run()

            {

                //synchronized(TraditionalThreadWaitAndNotify.class){

                    for(int i=0;i<50;i++){

                        bu.sub3(i);

                    }

                //}

            }

        }.start();

        for(int i=0;i<50;i++){

            bu.main(i);

        }

    }

}

class Business2{

    private Lock rl=new ReentrantLock();

    //private Condition condition=rl.newCondition();

    private Condition firstCondition=rl.newCondition();

    private Condition secondCondition=rl.newCondition();

    private Condition thirdCondition=rl.newCondition();

    private int sub =2;//让老2先走

    public  void sub2(int i){

        rl.lock();

        try

        {

            while(sub!=2){//while比if更健壮,while会判断两次

                try

                {

                    //this.wait();

//                    condition.await();

                    secondCondition.await();

                }

                catch (InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

            for(int j=0;j<10;j++){

                System.out.println("sub2 Thread :"+j+" of "+i);

            }

            sub=3;

            //this.notify();

            thirdCondition.signal();

        }

        catch (Exception e)

        {

            // TODO: handle exception

        }finally{

            rl.unlock();

        }

    }

    public  void sub3(int i){

        rl.lock();

        try

        {

            while(sub!=3){//while比if更健壮,while会判断两次

                try

                {

                    //this.wait();

                    thirdCondition.await();

                }

                catch (InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

            for(int j=0;j<10;j++){

                System.out.println("sub3 Thread :"+j+" of "+i);

            }

            sub=1;

            //this.notify();

            firstCondition.signal();

        }

        catch (Exception e)

        {

            // TODO: handle exception

        }finally{

            rl.unlock();

        }

    }

    public  void main(int i){

        rl.lock();

        try

        {

            while(sub!=1){//while比if更健壮,while会判断两次

                try

                {

                    //this.wait();

                    firstCondition.await();

                }

                catch (InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

            for(int j=0;j<100;j++){

                System.out.println("main thread :"+j+" of "+i);

            }

            sub=2;

            //this.notify();

            secondCondition.signal();

        }

        catch (Exception e)

        {

            // TODO: handle exception

        }finally{

            rl.unlock();

        }

    }

}

继续阅读