多线程测试类
Posted math-and-it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程测试类相关的知识,希望对你有一定的参考价值。
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();
以上是关于多线程测试类的主要内容,如果未能解决你的问题,请参考以下文章