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

public class ConditionThreeCommunicationTest
{
    /**
     * Condition类似于Object对象的wait和notify方法的功能(通信)
     * 可以使用多个Condition实现多个线程间的相互通信
     * */
    
    
    
    
    /**
     * 一个重要的设计思想(高内聚):
     *
     *     把有关联(用到了同一份数据(锁也是资源),或共同算法(计算公式))的若干个方法应该归在一个类身上,
     * 这种设计正好体现了高类聚和程序的健壮性
     *
     * ----锁是上在代表要操作的资源的类的内部方法中,而不是线程方法中!
     * */
    
    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++){
                        /*for(int j=0;j<10;j++){
                            System.out.println("sub Thread :"+j+" of "+i);
                        }*/
                        bu.sub2(i);
                    }
                //}
            }
        }.start();
        new Thread(){
            @Override
            public void run()
            {
                //synchronized(TraditionalThreadWaitAndNotify.class){
                    for(int i=0;i<50;i++){
                        /*for(int j=0;j<10;j++){
                            System.out.println("sub Thread :"+j+" of "+i);
                        }*/
                        bu.sub3(i);
                    }
                //}
            }
        }.start();
        /*for(int i=0;i<100;i++){
            System.out.println("main thread :"+i);
        }*/
        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();
        }
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值