码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程并发

时间:2014-08-08 12:02:25      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   for   art   ar   

    @Test
    public void testRunBatchClient() {
        long start = System.currentTimeMillis();
        runBatchClient(30);
        long end = System.currentTimeMillis();
        System.out.println("子线程执行时长:" + (end - start));

    }

    private void runBatchClient(int clients){
        CountDownLatch countDownLatch = new CountDownLatch(clients);
        ClientMock clientMock = new ClientMock(countDownLatch);
        for (int i = 0; i < clients; i++) {
            Thread thread = new Thread(clientMock);
            thread.start();
        }
        try {
            // 阻塞当前线程,直到倒数计数器倒数到0
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    class ClientMock implements Runnable {
        private CountDownLatch countDownLatch;

        public ClientMock(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "子线程开始");
            long start = System.currentTimeMillis();
            try {

                UploadZIP();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
            long end = System.currentTimeMillis();
            System.out.println(Thread.currentThread().getName() + "子线程结束,时长:" + (end - start));
        }
    }

 

java多线程并发,布布扣,bubuko.com

java多线程并发

标签:style   blog   color   java   io   for   art   ar   

原文地址:http://www.cnblogs.com/puqiuxiaomao/p/3898810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!