中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專題文章博客和開源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Java實(shí)現(xiàn)主線程等待子線程

            本文介紹兩種主線程等待子線程的實(shí)現(xiàn)方式,以5個(gè)子線程來說明:
            1、使用Thread的join()方法,join()方法會(huì)阻塞主線程繼續(xù)向下執(zhí)行。
            2、使用java.util.concurrent中的CountDownLatch,是一個(gè)倒數(shù)計(jì)數(shù)器。初始化時(shí)先設(shè)置一個(gè)倒數(shù)計(jì)數(shù)初始值,每調(diào)用一次countDown()方法,倒數(shù)值減一,他的await()方法會(huì)阻塞當(dāng)前進(jìn)程,直到倒數(shù)至0.
            join方式代碼如下:
            package com.test.thread;
            import java.util.ArrayList;
            import java.util.List;
            public class MyThread extends Thread
            {
            public MyThread(String name)
            {
            this.setName(name);
            }
            @Override
            public void run()
            {
            System.out.println(this.getName() + " staring…");
            System.out.println(this.getName() + " end…");
            }
            /**
            * @param args
            */
            public static void main(String[] args)
            {
            System.out.println("main thread starting…");
            List<MYTHREAD> list = new ArrayList<MYTHREAD>();
            for (int i = 1; i <= 5; i++)
            {
            MyThread my = new MyThread("Thrad " + i);
            my.start();
            list.add(my);
            }
            try
            {
            for (MyThread my : list)
            {
            my.join();
            }
            }
            catch (InterruptedException e)
            {
            e.printStackTrace();
            }
            System.out.println("main thread end…");
            }
            }
            運(yùn)行結(jié)果如下:
            main thread starting…
            Thrad 2 staring…
            Thrad 2 end…
            Thrad 4 staring…
            Thrad 4 end…
            Thrad 1 staring…
            Thrad 1 end…
            Thrad 3 staring…
            Thrad 3 end…
            Thrad 5 staring…
            Thrad 5 end…
            main thread end…
            CountDownLatch方式代碼如下:
            package com.test.thread;
            import java.util.concurrent.CountDownLatch;
            public class MyThread2 extends Thread
            {
            private CountDownLatch count;
            public MyThread2(CountDownLatch count, String name)
            {
            this.count = count;
            this.setName(name);
            }
            @Override
            public void run()
            {
            System.out.println(this.getName() + " staring…");
            System.out.println(this.getName() + " end…");
            this.count.countDown();
            }
            /**
            * @param args
            */
            public static void main(String[] args)
            {
            System.out.println("main thread starting…");
            CountDownLatch count = new CountDownLatch(5);
            for (int i = 1; i <= 5; i++)
            {
            MyThread2 my = new MyThread2(count, "Thread " + i);
            my.start();
            }
            try
            {
            count.await();
            }
            catch (InterruptedException e)
            {
            e.printStackTrace();
            }
            System.out.println("main thread end…");
            }
            }
            運(yùn)行結(jié)果如下:
            main thread starting…
            Thread 2 staring…
            Thread 2 end…
            Thread 4 staring…
            Thread 4 end…
            Thread 1 staring…
            Thread 1 end…
            Thread 3 staring…
            Thread 3 end…
            Thread 5 staring…
            Thread 5 end…
            main thread end…托福答案

          posted on 2014-08-25 22:00 好不容易 閱讀(201) 評(píng)論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開獎(jiǎng) PK10開獎(jiǎng)
          主站蜘蛛池模板: 织金县| 固镇县| 广饶县| 耿马| 拉萨市| 鹿邑县| 娱乐| 威海市| 大理市| 邯郸市| 庐江县| 开原市| 永定县| 金乡县| 临高县| 新兴县| 九江县| 鄂托克前旗| 南漳县| 桦川县| 大同县| 黄龙县| 长沙市| 三河市| 岳阳市| 秦皇岛市| 景东| 垦利县| 湄潭县| 四子王旗| 青川县| 唐河县| 三台县| 大同县| 余江县| 慈利县| 张家界市| 屯留县| 旌德县| 漳平市| 青田县|