long ls = System.currentTimeMillis();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(30);
for (int i = 0; i < 2000; i++) {
final int index = i;
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("thread start" + (index+1));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread end" + (index+1));
}
});
}
fixedThreadPool.shutdown();
try {
// 设置等待所有子任务完成
fixedThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
System.out.println("时间"+(System.currentTimeMillis()-ls));
} catch (InterruptedException e) {
e.printStackTrace();
}
newFixedThreadPool创建固定线程
猜你喜欢
转载自blog.csdn.net/weikzhao0521/article/details/80162958
今日推荐
周排行