JAVA 线程池处理大量数据
package com.hongfu.spring.Thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* java线程池处理大量数据
*/
public class ThreadPoolHandler {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("测试1");
list.add("测试2");
list.add("测试3");
list.add("测试4");
list.add("测试5");
list.add("测试6");
list.add("测试7");
list.add("测试8");
list.add("测试9");
list.add("测试10");
ExecutorService executorService = Executors.newFixedThreadPool(list.size());
for (String str : list) {
executorService.execute(() -> {
//线程内操作
System.out.println("正在处理"+str);
});
}
executorService.shutdown();
while(true){
//线程执行完毕进行操作
if(executorService.isTerminated()){
System.out.println("我出现了一次 兄弟");
break;
}
}
}
}