Concurrent包同步器模擬程序

【前言】:java.util.concurrent包中高級的工具分爲三類:Executor frameWork、性能更好的併發容器(ConcurrentHashMap)以及同步器(使線程能夠等待另一個線程的對象),比較常用的同步器就是CountDownLatch和Semaphore

import org.junit.Test;
import java.util.concurrent.CountDownLatch;

/**
 * @Author : 虐鱷狂人薛定貓
 * @Date : 2016/9/1 15:37
 * @Version : 1.0.0
 * @Description:三個同步器,ready、start、stop
 * 所有線程ready後,開始計時,start啓動,等待所有線程stop,,計時結束
 */

public class CountdowmTest {

    final static CountDownLatch ready=new CountDownLatch(3);
    final static CountDownLatch start=new CountDownLatch(1);
    final static   CountDownLatch stop=new CountDownLatch(3);

    /**
     * ready 最後一個線程全部countdown後,所以線程準備完畢,開始計時,進入start.countdown(),允許所有工作繼續進行,
     * 然後線程在第三個鎖存器上等待,直到完成countdown ,線程甦醒記錄下結束時間
     * @param args
     */
    public static void main(String[] args){
        System.out.println("result:"+CountTest());
    }


    public static long CountTest() {
        int i;
        for(i=0; i<3; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ready.countDown();
                    try{
                        start.await();
                      //  System.out.println("start:=="+ready.);
                    }catch (Exception e){
                        e.printStackTrace();
                    }finally{
                        stop.countDown();
                    }
                }
            }).start();
        }

        try{
            long l1=System.nanoTime();
            ready.await();//wait all thread ready
            System.out.println(System.nanoTime()-l1);//等待的時間
        }
        catch (Exception e){
            e.printStackTrace();
        }

        long startTime=System.nanoTime();
        System.out.println("=======start==========");
        start.countDown();//start
        try{
            stop.await();//wait all thread done
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("=======stop==========");
        return System.nanoTime()-startTime;

    }
}

import java.util.concurrent.Semaphore;

/**
 * @Author : 虐鱷狂人薛定貓
 * @Date : 2016/9/1 16:38
 * @Version : 1.0.0
 */

public class SemaphoreTest {
    /**Semaphore翻譯成字面意思爲 信號量,Semaphore可以控同時訪問的線程個數,
     * 通過 acquire() 獲取一個許可,如果沒有就等待,而 release() 釋放一個許可。
     *  假若一個工廠有5臺機器,但是有8個工人,
     * 一臺機器同時只能被一個工人使用,只有使用完了,其他工人才能繼續使用。那麼我們就可以通過Semaphore來實現:
     * @param args
     */
    public static void main(String[] args) {
        int N = 8;            //工人數
        Semaphore semaphore = new Semaphore(5); //機器數目 即同時可以允許多少線程進行訪問
        for(int i=0;i<N;i++)
            new Worker(i,semaphore).start();
    }

    static class Worker extends Thread{
        private int num;
        private Semaphore semaphore;
        public Worker(int num,Semaphore semaphore){
            this.num = num;
            this.semaphore = semaphore;
        }

        @Override
        public void run() {
            try {
                semaphore.acquire();//獲取一個許可
                System.out.println("work "+this.num+" acquire this machine...");
                Thread.sleep(2000);
                System.out.println("work "+this.num+" release this machine");
                semaphore.release();//釋放一個許可
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


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