//開始多線程任務
public Map<String, String> start(List<PddAccount> list) throws InterruptedException {
CountDownLatch count = new CountDownLatch(list.size());
Map<String, String> tasks = tasks(list, count);
return tasks;
}
//執行多線程任務
public Map<String,String> tasks(List<PddAccount> list, CountDownLatch count) throws InterruptedException {
Map<String,String> map=new HashMap<>();
ExecutorService pool = Executors.newFixedThreadPool(30);
for (int i = 0; i < list.size(); i++) {
PddAccount str = list.get(i);
Runnable run = new Runnable() {
public void run() {
try {
//需要多線程執行的代理
System.out.println(Thread.currentThread().getName()+"___"+str.getAccount());
map.put(Thread.currentThread().getName(),"true");//模擬返回結果
} catch (Exception e) {
e.printStackTrace();
}
count.countDown();
}
};
pool.submit(run);
}
pool.shutdown();
//所有線程提交完,等待所有線程結束
count.await();//阻塞在這裏,等到所有線程返回結果
return map;
}