協調線程間同步的類之三----Exchanger 學習筆記

一。java

        1>exchanger讓兩個線程能夠互換信息。下面例子表示服務生線程往空的杯子中倒水,顧客線程從裝滿水的杯子裏喝水。而後經過Exchanger雙方互換杯子,服務生接着往空杯子裏倒水,顧客接着喝水。周而復始ide

        2>注意,一個exchanger只能讓兩個線程互換信息,線程多了不行,當一個線程到exchanger.exchange()方法而另外一個還沒到的時候,線程會處於阻塞狀態,直到另外一個也到了以後一塊兒執行。
測試

        3>我的感受用處不大,測試代碼以下this

public class ExchangerTest {
    public static class Cup{
        private boolean isfull=false;
        public Cup(boolean isfull){
            this.isfull=isfull;
        }
        //添水用5s
        public void addWater(){
            if(!this.isfull){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.isfull=true;
            }
        }
        
        public void drinkWater(){
            if(this.isfull){
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                isfull=false;
            }
        }
    }
    
    public static void  testExchanger() {
        final Exchanger<Cup> exchanger=new Exchanger<Cup>();//姑且叫他交換機 吧
        final Cup initailEmptyCup=new Cup(false);//初始的空杯子
        final Cup initailFullCup=new Cup(true);//初始的滿杯子
        class Waiter implements Runnable{
            @Override
            public void run() {
                Cup currentCup=initailEmptyCup;
                int i=0;
                while(i<2){
                    System.out.println("服務生添水:"+i);
                    currentCup.addWater();
                    System.out.println("服務生添水完畢,等待交換杯子");
                    try {
                        currentCup=exchanger.exchange(currentCup);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("交換杯子完畢"+System.currentTimeMillis());
                    i++;
                }
            }
        }
        class Consumer implements Runnable{
            @Override
            public void run() {
                Cup cup=initailFullCup;
                int i=0;
                while(i<2){
                    System.out.println("顧客開始喝水:"+i);
                    cup.drinkWater();
                    System.out.println("顧客喝水完畢,等待交換杯子");
                    try {
                        cup=exchanger.exchange(cup);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("顧客與服務生交換杯子完畢"+System.currentTimeMillis());
                    i++;
                }
            }
        }
        new Thread(new Waiter()).start();
        new Thread(new Consumer()).start();
    }
    public static void main(String[] args) {
        testExchanger();
    }
}
相關文章
相關標籤/搜索