哈哈,我知道这样,我现在就是数据量巨大,想多线程执行,加快速度,所以用的多线程,我的例子只是个看起方便而已
@Test
public void test5(){
List<Map> maps = new ArrayList<>();
Chain chain = Chain.make("name","12312312313");
Map map = new HashMap();
map.put("chain",chain);
map.put("tableName","text_l");
maps.add(map);
for (int i = 0; i < 500000; i++) {
maps.add(map);
}
List<List<Map>> lists = ListUtil.split(maps,1000);//把集合分成等分的多个集合
int i = 0;//0成功 1 失败
try {
Trans.begin();//开起事务
try {//开始做事
// 创建一个线程池
ThreadPoolExecutor tpl = new
ThreadPoolExecutor(0, maps.size(), 60L, TimeUnit.SECONDS,new SynchronousQueue<>());
try {
ArrayList<Future<Boolean>> al = new ArrayList();
for (List<Map> sqlList1 :lists) {
// 创建有返回值的任务
al.add(tpl.submit(new TextThread(dao,sqlList1)));
}
for(Future<Boolean> future:al){
while (true) {
/**
* 获得future对象之前可以使用isDone()方法检测future是否完成,完成后可以调用get()方法获得future的值,
* 如果直接调用get()方法,get()方法将阻塞值线程结束
*/
if (future.isDone()) {
try {
i = future.get() == false ? 1:0;
break;
} catch (InterruptedException e) {
i = 1;
break;
} catch (ExecutionException e) {
i = 1;
break;
}
}
}
if (i == 1) {
break;
}
}
} finally {
// 关闭线程池
tpl.shutdown();
}
if (i == 1) {
throw new Exception();
}
// 做完了
Trans.commit();//提交事务
} catch (Exception ee) {
Trans.rollback();//出错了,回滚
} finally {
Trans.close();//关闭事务
}
} catch (Exception e) {
e.printStackTrace();
i = 1;
}
System.out.println(i == 0 ? "成功" : "失败");
}
public class TextThread implements Callable<Boolean>{
protected static final Log log = Logs.get();
private Ioc ioc;
private Dao dao;
private List<Map> maps;
public TextThread(Dao dao,List<Map> maps) {
this.dao = dao;
this.maps = maps;
}
@Override
public Boolean call() throws Exception {
int i;
Chain chain;
String tableName;
for (Map map :maps) {
chain = (Chain)map.get("chain");
tableName = map.get("tableName").toString();
try {
dao.insert(tableName,chain);
i = 0;
} catch (Exception e) {
i = 1;
}
if (i == 1){
return false;
}
}
return true;
}
}