ExecutorService线程池应用
//线程数量
int threadNum = lists.size();
//创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(threadNum);
for (int i = 0; i < threadNum; i++) {
final List<ChannelInfo> c = lists.get(i);
final int currentThreadNum = i;
pool.execute(new Runnable() {
public void run() {
long begin = System.currentTimeMillis();
try{
//逻辑编写
beanList.addAll(batchBeansByInfos(c));
//业务逻辑结束
}catch (Exception e){
e.printStackTrace();
}finally {
long end = System.currentTimeMillis();
System.out.println("线程"+currentThreadNum+"导出耗时:" + (end - begin)+",数据条数:"+beanList.size());
}
}
});
}
pool.shutdown();
while(true){
if(pool.isTerminated()){
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("导出结束");
}
itjiandan