【JAVA並發編程實戰】4、CountDownLatch

來源:互聯網
上載者:User

標籤:位置   generate   tar   編程   row   空格   stack   運行   static   

這是一個計數鎖,說白了,就是當你上鎖的時候,只有計數減少到0的時候,才會釋放鎖

 

package cn.xf.cp.ch05;public class TaskRunable implements Runnable{    @Override    public void run()    {        System.out.println("do something...");    }        public static void main(String []args)    {        Thread t = new Thread(new TaskRunable());        t.start();    }}

 

package cn.xf.cp.ch05;import java.util.concurrent.CountDownLatch;/** *  *功能:10個線程同步 *時間:下午6:08:46 *檔案:ReadData.java  *@author Administrator * */public class ReadData{//    private static int SEEK_NUM = 0;        //同步運行,並統計時間    public static long timeTasks(int nThreads, final Runnable task) throws InterruptedException    {        //起始門,用來劃定線程同步開始的時間        final CountDownLatch startGate = new CountDownLatch(1);        //用來等待所有線程結束        final CountDownLatch endGate = new CountDownLatch(nThreads);                for(int i = 0; i < nThreads; ++i)        {            //設定寫出檔案開始位置,一個long+空格是10個位元組            //建立線程            Thread t = new Thread()                                {                                    public void run()                                    {                                        try                                        {                                            //等待所有線程啟動                                            startGate.await();                                            try                                            {                                                task.run();//啟動線程                                            }                                            finally                                            {                                                //線程運行結束之後上通知                                                endGate.countDown();                                            }                                        }                                        catch (InterruptedException e)                                        {                                        }                                    }                                };            t.start();        }                long start = System.nanoTime();        //使開始門解鎖,task開始工作        startGate.countDown();        //給end們上鎖,只有減少到0才解鎖        endGate.await();        long end = System.nanoTime();                return end - start; //時間結果    }        public static void main(String[] args)    {        TaskRunable tr = new TaskRunable();        try        {            System.out.println(ReadData.timeTasks(10, tr));        }        catch (InterruptedException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

 

結果

 

【JAVA並發編程實戰】4、CountDownLatch

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.