您的位置:首页 > 其它

多线程计数器之CountDownLatch和join

2017-07-17 11:24 471 查看
[java] view
plain copy

 print?

/** 

 * 这个是实现计数器操作,主线程等待子线程全部完成 

 * 模拟场景:10个人赛跑,当所有的人到达终点后,比赛结束 

 * @author zhangm 

 * 

 */  

public class TestCountDownLatch  {  

  

     //计数器,从2开始计数  

     private final static CountDownLatch mCountDownLatch = new CountDownLatch(3);  

       

     /** 

         * 示例工作线程类 

         */  

        private static class WorkingThread extends Thread {  

            private final String mThreadName;  

            private final int mSleepTime;  

            public WorkingThread(String name, int sleepTime) {  

                mThreadName = name;  

                mSleepTime = sleepTime;  

            }  

              

            @Override  

            public void run() {  

                System.out.println("[" + mThreadName + "] started!");  

                try {    

                        Thread.sleep(mSleepTime);    

                } catch (InterruptedException e) {    

                        e.printStackTrace();    

                }  

                mCountDownLatch.countDown();  

                System.out.println("[" + mThreadName + "] end!");   

            }  

        }  

          

        /** 

         * 示例线程类 

        
16be9
 */  

        private static class SampleThread extends Thread {  

              

            @Override  

            public void run() {  

                System.out.println("[SampleThread] started!");  

                try {  

                    // 会阻塞在这里等待 mCountDownLatch 里的count变为0;  

                    // 也就是等待另外的WorkingThread调用countDown()  

                    mCountDownLatch.await();  

                } catch (InterruptedException e) {  

                      

                }  

                System.out.println("[SampleThread] end!");  

            }  

        }  

          

        public static void main(String[] args) throws Exception {  

            // 最先run SampleThread  

            new SampleThread().start();  

            // 运行工作线程  

            new WorkingThread("WorkingThread1", 1000).start();  

            new WorkingThread("WorkingThread2", 2000).start();  

            new WorkingThread("WorkingThread3", 1000).start();  

        }  

      

}   

执行结果如下:

[SampleThread] started!

[WorkingThread1] started!

[WorkingThread2] started!

[WorkingThread3] started!

[WorkingThread1] end!

[WorkingThread2] end!

[WorkingThread3] end!

[SampleThread] end!

另外还可以使用join使得主线程等待子线程完成。

[java] view
plain copy

 print?

public class TestJoin extends Thread{  

  

    private  String name;  

    private  int  time;  

      

    public TestJoin(String name,int time)  

    {  

        this.name=name;  

        this.time=time;  

    }  

  

    //子线程执行部分  

    @Override  

    public void run()  

    {  

        System.out.println(this.getName() + " staring...");  

        try {    

            Thread.sleep(time);    

        } catch (InterruptedException e) {    

                e.printStackTrace();    

        }  

        System.out.println(this.getName() + " end...");  

    }  

  

      

    public static void main(String[] args)  

    {  

        System.out.println("main thread starting...");  

  

        List<TestJoin> list = new ArrayList<TestJoin>();  

  

        int time;  

        for (int i = 1; i <= 5; i++)  

        {  

            time=(int) (Math.random()*3+1);  

            TestJoin my = new TestJoin("Thrad " + i,1000*time);  

            my.start();  

            list.add(my);  

        }  

  

        try  

        {  

            for (TestJoin my : list)  

            {  

                my.join();  

            }  

        }  

        catch (InterruptedException e)  

        {  

            e.printStackTrace();  

        }  

  

        System.out.println("main thread end...");  

  

    }  

  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: