java多线程
使用CompletionService提交异步任务,直接上代码
final int count = 1000;
public static final ExecutorService executorService = Executors.newFixedThreadPool(3);
public void deal(){
CompletionService cs = new ExecutorCompletionService(executorService);
for(i=0;i<100;i++){
List<Object> datalist = dao.selectList("select * tableName where state = 0");
cs.submit(datalist)
}
CountDownLatch latch = new CountDownLatch(100);
for(i=0;i<100;i++){
executorService.submit(()->{
dao.updateState(update tableName set state = 1 limit i,1);
latch.countDown();
});
}
latch.await();
if(count - 100 > 0){
//此处调用的全是和第一次的一模一样
deal();
}
}
内层调用获取到的数据全部一模一样,尝试过加读写锁,都不生效,求助