java多线程管理 concurrent包用法详解,所有线程执行完成时再执行余下的内容

//list 100条每组切分
List<List<WendaQuestion>> allList = getAllList(insetQuestionList, 100);
		if (allList != null && !allList.isEmpty()) {
			ExecutorService service = Executors.newFixedThreadPool(10);
			CountDownLatch cd = new CountDownLatch(allList.size());
			for (int i = 0; i < allList.size(); i++) {

				service.submit(new QuestionExecutor(wendaQuestionDao,
						wendaQuestionCategoryRefDao, wendaRuleQuestionRefDao,
						allList.get(i), creator, wendaRule, cd));

				
			}
			try {
				cd.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			service.shutdown();
		}

<pre name="code" class="java">//切分list
public List<List<WendaQuestion>> getAllList(List<WendaQuestion> list,
int size) {
List<List<WendaQuestion>> allList = new ArrayList<List<WendaQuestion>>();
List<WendaQuestion> subList = new ArrayList<WendaQuestion>();
Iterator<WendaQuestion> iter = list.iterator();
while (iter.hasNext()) {
subList.add((WendaQuestion) iter.next());
if (subList.size() == size) {
allList.add(subList);
subList = new ArrayList<WendaQuestion>();
}
}
allList.add(subList);
return allList;
}//线程static class QuestionExecutor extends Thread {
private WendaQuestionDao wendaQuestionDao;
private WendaQuestionCategoryRefDao wendaQuestionCategoryRefDao;
private WendaRuleQuestionRefDao wendaRuleQuestionRefDao;
private List<WendaQuestion> questionlist = new ArrayList<WendaQuestion>();
private String creator;
private WendaRule wendaRule;
CountDownLatch latch;


private QuestionExecutor(WendaQuestionDao wendaQuestionDao,
WendaQuestionCategoryRefDao wendaQuestionCategoryRefDao,
WendaRuleQuestionRefDao wendaRuleQuestionRefDao,
List<WendaQuestion> questionlist, String creator,
WendaRule wendaRule, CountDownLatch latch) {
super();
this.wendaQuestionDao = wendaQuestionDao;
this.wendaQuestionCategoryRefDao = wendaQuestionCategoryRefDao;
this.wendaRuleQuestionRefDao = wendaRuleQuestionRefDao;
this.questionlist = questionlist;
this.creator = creator;
this.wendaRule = wendaRule;
this.latch = latch;
}


public QuestionExecutor() {
}


public void run() {
this.insert();
latch.countDown();
}


public void insert() {
Iterator<WendaQuestion> it1 = questionlist.iterator();
while (it1.hasNext()) {
try {
WendaQuestion question = (WendaQuestion) it1.next();
wendaQuestionDao.insert(question);
// 增加问题分类关联
WendaQuestionCategoryRef categoryRef = new WendaQuestionCategoryRef();
categoryRef.setCatagoryId(wendaRule.getCategoryId());
categoryRef.setQuestionId(question.getId());
wendaQuestionCategoryRefDao.insert(categoryRef);
// 增加问题规则关联
WendaRuleQuestionRef ref = new WendaRuleQuestionRef();
ref.setCreator(creator);
ref.setCreatedAt(new Date());
ref.setGenerateId(wendaRule.getId());
ref.setQuestionId(question.getId());
wendaRuleQuestionRefDao.insert(ref);


} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}


}


}
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值