标签:多线程实例
@Component public class UserInsertThread implements Runnable { private List<User> list; private CountDownLatch latch; public UserInsertThread(CountDownLatch latch, List<User> list) { this.latch = latch; this.list = list; } public void run() { insert(); } // @Transactional(propagation = Propagation.NESTED) @Transactional public void insert() { UserMapper userMapper1 = SpringBeanFactory.getBean(UserMapper.class); userMapper1.insertBatch(list); this.latch.countDown(); } } @Transactional
public Integer insertBatch(List<User> list) {
int cnt = list.size();
int threadCnt = cnt / 500;
if(cnt%500 > 0) {
threadCnt++;
}
final CountDownLatch cdl= new CountDownLatch(threadCnt);
List<User> threadList = null;
for (int i = 0; i < threadCnt; i++) {
threadList = i == threadCnt-1 ? list.subList(i * 500, list.size()-1) : list.subList(i*500, (i+1)*500);
new Thread(new UserInsertThread(cdl, threadList)).start();
}
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}标签:多线程实例
原文地址:http://blog.51cto.com/xinzhilian/2089523