傳統方式實現三個線程之間的通信: 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();
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章