线程池模拟高并发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Integer taskCount =700;
// 锁住所有线程,等待并发执行
final CountDownLatch begin =new CountDownLatch(1);
final ExecutorService exec =Executors.newFixedThreadPool(taskCount);
for (int index =0; index < taskCount; index++){
final int NO= index +1;
Runnable run =new Runnable() {
@Override
public void run() {
try {
// 等待,所有一起执行
begin.await();
//开始模拟等待。。。
//Thread.sleep((long) (Math.random() * 10000));
}catch (InterruptedException e){
e.printStackTrace();
}finally {
}
}
};
exec.submit(run);
}
System.out.println("开始执行");
// begin减一,开始并发执行
begin.countDown();
//关闭执行
exec.shutdown();
Jeff-Eric wechat