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();
}
}