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

多线程测试类

时间:2019-08-24 15:07:03      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:bsp   interface   exception   stack   any   for   nts   rup   current   

package base_class;

import java.util.concurrent.CountDownLatch;

/**
 * 多线程测试器
 */
public class ManyThreadStarter {

    private int count;

    public ManyThreadStarter(){
        count = 10000;
    }

    public ManyThreadStarter(int count){
        this.count = count;
    }

    public void allThreadRun(Run run){
        CountDownLatch latch = new CountDownLatch(count);

        long start = System.currentTimeMillis();
        for(int i=0;i<count;i++){
            new Thread(()->{
                try{
                    run.run();
                }catch (Exception e) {
                  e.printStackTrace();
                } finally {
                    latch.countDown();
                }
            }).start();
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行耗时:"+(System.currentTimeMillis()-start)+"毫秒");

    }

    public interface Run{
        void run();
    }

}

 

多线程测试类

标签:bsp   interface   exception   stack   any   for   nts   rup   current   

原文地址:https://www.cnblogs.com/math-and-it/p/11404463.html

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