免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2243 | 回复: 0
打印 上一主题 下一主题

主线程等待子线程完成的实现[join , wait-notify,CountDownLatch]方式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-12 11:45 |只看该作者 |倒序浏览

import java.util.concurrent.CountDownLatch;
public class TestThread
{
    static int cnt = 100;
    static int last = -1;
    static int type = 2;
    static int liveCnt = cnt;
    static CountDownLatch counter = new CountDownLatch(cnt);
    static class WorkThread extends Thread
    {
        Object lock = null;
        int id = -1;
        int sleep = -1;
        WorkThread(int id, Object lock)
        {
            this.id = id;
            this.lock = lock;
        }
        public void run()
        {
            try
            {
                work();
            } catch (Exception e)
            {
            }
            synchronized (lock)
            {
                //System.out.println("thread " + id + " done");
                last = sleep;
                liveCnt--;
                counter.countDown();
                lock.notify();
            }
        }
        private void work()
        {
            try
            {
                sleep = (int) (Math.random() * 1000 * 10);
                //System.out.println("thread " + id + " sleep=" + sleep);
                Thread.sleep(sleep);
            } catch (InterruptedException e)
            {
            }
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String lock = "lock";
        long start = System.currentTimeMillis();
        WorkThread[] workers = new WorkThread[cnt];
        for (int i = 0; i  cnt; i++)
        {
            workers = new WorkThread(i, lock);
            workers.start();
        }
        switch (type)
        {
            //join 方式
            case 1 :
                for (Thread worker : workers)
                {
                    try
                    {
                        worker.join();
                    } catch (InterruptedException e)
                    {
                    }
                }
                break;
            //wait - notify 方式   
            case 2 :
                while (true)
                {
                    synchronized (lock)
                    {
                        if (liveCnt == 0)
                            break;
                        try
                        {
                            lock.wait();
                        } catch (InterruptedException e)
                        {
                        }
                    }
                }
                //CountDownLatch 方式
            case 3 :
                try
                {
                    counter.await();
                } catch (InterruptedException e)
                {
                }
                break;
        }
        long end = System.currentTimeMillis();
        System.out.println("type=" + type);
        System.out.println("All done, elapse: " + (end - start));
        System.out.println("last=" + last);
        System.out.println("delay=" + (end - start - last));
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17518/showart_1715528.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP