传统方式实现三个线程之间的通信: notify与notifyAll

使用notify方法只唤醒一个线程(任意唤醒一个),

而notifyAll则唤醒该对象监视器上等待的所有线程,被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争

/这里用notify会有问题,但是用notifyAll就可以实现.但对于notifyAll我还是不太懂

public class TraditionalThreeCommunicationTest

{
    /**
     * 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 int sub = 2;// 让老2先走

    public synchronized void sub2(int i)
    {
        while (sub != 2)
        {// while比if更健壮,while会判断两次
            try
            {
                this.wait();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        for (int j = 0; j < 10; j++)
        {
            System.out.println("sub2 Thread :" + j + " of " + i);
        }
        sub = 3;
//        this.notify();
        this.notifyAll();

    }

    public synchronized void sub3(int i)
    {
        while (sub != 3)
        {// while比if更健壮,while会判断两次
            try
            {
                this.wait();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        for (int j = 0; j < 10; j++)
        {
            System.out.println("sub3 Thread :" + j + " of " + i);
        }
        sub = 1;
        //this.notify();
        this.notifyAll();

    }

    public synchronized void main(int i)
    {
        while (sub != 1)
        {// while比if更健壮,while会判断两次
            try
            {
                this.wait();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

        }
        for (int j = 0; j < 100; j++)
        {
            System.out.println("main thread :" + j + " of " + i);
        }
        sub = 2;
        this.notifyAll();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值