实现功能
1.使用线程池执行一个任务List,
2.等待所有线程执行结果完成后返回结果
3.触发线程池创建新线程。
4.线程池关闭线程时非立即关闭验证。
//Thread 类
@Data
public class TaskDemo implements Runnable{
//队列信息存储服务需要返回的结果
private CountDownLatch countDownLatch;
private Map outMap;
@Override
public void run() {
try {
Double randomNumber = (Math.random() * 10);
int sleepSS = randomNumber.intValue();
TimeUnit.SECONDS.sleep(sleepSS);
System.out.println("线程执行:========="+Thread.currentThread().getName()+"===============");
if (sleepSS%2==1) {
outMap.put("resultCode","1");
outMap.put("resultMsg",Thread.currentThread().getName()+"===1");
}else {
outMap.put("resultCode", "0");
outMap.put("resultMsg",Thread.currentThread().getName()+"===0");
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
if (countDownLatch != null) {
System.out.println("线程执行结束:========="+Thread.currentThread().getName()+"===============");
countDownLatch.countDown();
}
}
}
}
main函数
public static void main( String[] args ) throws InterruptedException {
List<String> taskList= new ArrayList();
for (int i = 0; i < 15; i++) {
taskList.add(i+"");
}
Map innerMap = new HashMap();
List<TaskDemo> TaskDemos = new ArrayList<TaskDemo>();
LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(5);
CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
ExecutorService pool =
new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, linkedBlockingQueue);
for (String s : taskList) {
Map outMap = new HashMap();
TaskDemo runnable = new TaskDemo();
runnable.setCountDownLatch(countDownLatch);
runnable.setOutMap(outMap);
pool.execute(runnable);
TaskDemos.add(runnable);
}
countDownLatch.await();
Long thisTime = System.currentTimeMillis();
pool.shutdown();
System.out.println(Thread.activeCount());
System.out.println("End");
while (Thread.activeCount()>2){
System.out.println("Thread is 大于2");
}
System.out.println("Thread is 2");
System.out.println(System.currentTimeMillis()-thisTime);
for (TaskDemo demo : TaskDemos) {
System.out.println(MapUtils.getString(demo.getOutMap(),"resultMsg"));
}
// TimeUnit.SECONDS.sleep(5);
// System.out.println("End");
// System.out.println(Thread.activeCount());
// System.out.println("End");
}
控制台打印
线程执行:=========pool-1-thread-4===============
线程执行结束:=========pool-1-thread-4===============
线程执行:=========pool-1-thread-7===============
线程执行结束:=========pool-1-thread-7===============
线程执行:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-10===============
线程执行结束:=========pool-1-thread-10===============
线程执行:=========pool-1-thread-7===============
线程执行结束:=========pool-1-thread-7===============
线程执行:=========pool-1-thread-3===============
线程执行结束:=========pool-1-thread-3===============
线程执行:=========pool-1-thread-8===============
线程执行:=========pool-1-thread-2===============
线程执行结束:=========pool-1-thread-2===============
线程执行:=========pool-1-thread-9===============
线程执行结束:=========pool-1-thread-9===============
线程执行:=========pool-1-thread-5===============
线程执行结束:=========pool-1-thread-5===============
线程执行结束:=========pool-1-thread-8===============
线程执行:=========pool-1-thread-4===============
线程执行结束:=========pool-1-thread-4===============
线程执行:=========pool-1-thread-1===============
线程执行结束:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-6===============
线程执行:=========pool-1-thread-10===============
线程执行结束:=========pool-1-thread-10===============
12
End
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 2
1
pool-1-thread-1===0
pool-1-thread-2===0
pool-1-thread-3===1
pool-1-thread-4===0
pool-1-thread-5===0
pool-1-thread-4===1
pool-1-thread-7===1
pool-1-thread-6===1
pool-1-thread-1===1
pool-1-thread-10===0
pool-1-thread-6===0
pool-1-thread-7===1
pool-1-thread-8===0
pool-1-thread-9===0
pool-1-thread-10===1