public void threadTest() throws InterruptedException{
//访问数
int threadSize = 1000;
//最大并发数量
int poolSize = 100;
CountDownLatch countDownLatch1 = new CountDownLatch(1);
CountDownLatch countDownLatch2 = new CountDownLatch(threadSize);
ExecutorService executorService = new ThreadPoolExecutor(poolSize, poolSize,
10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(10000), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
//开始时间
long beginTime = System.currentTimeMillis();
boolean isBeginToRun = false;
for (int i = 0; i < threadSize; i++) {
//创建子线程并使其阻塞
executorService.execute(()->{
try {
System.out.println(Thread.currentThread().getName()+"---bigin");
//阻塞到countDownLatch1为0时在开始执行
countDownLatch1.await();
//需要执行的操作
// httpSend();
System.out.println(Thread.currentThread().getName()+"---END");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
countDownLatch2.countDown();
}
});
System.out.println("结束--"+i);
}
//使所有的子线程开始并发执行
countDownLatch1.countDown();
System.out.println("线程开始执行");
//阻塞主线程
countDownLatch2.await();
//结束时间
long endTime = System.currentTimeMillis();
//执行花费时间
System.out.println("thread size : "+threadSize+", use pool :"+(endTime-beginTime));
executorService.shutdown();
}