package com.suning.sntcscase.controller.thread;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.*;
import com.suning.sntcscase.entity.User;
import com.suning.sntcscase.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("con")
public class ConController {
private final static Logger logger = LoggerFactory.getLogger(Logger.class);
@Resource
private UserService userService;
@RequestMapping(value = "test1", method = RequestMethod.GET)
public String test1() {
try {
//10万条数据
List<User> list = userService.getAll();
List<User> list2 = new ArrayList<>();
long st = System.currentTimeMillis();
//每条线程处理的数据尺寸
int size = 250;
int count = list.size() / size;
if (count * size != list.size()) {
count++;
}
int countNum = 0;
final CountDownLatch countDownLatch = new CountDownLatch(count);
ExecutorService executorService = Executors.newFixedThreadPool(8);
ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
while (countNum < list.size()) {
countNum += size;
ConCallable callable = new ConCallable(userService, countNum);
//截取list的数据,分给不同线程处理
callable.setList(ImmutableList.copyOf(list.subList(countNum - size, countNum < list.size() ? countNum : list.size())));
ListenableFuture listenableFuture = listeningExecutorService.submit(callable);
Futures.addCallback(listenableFuture, new FutureCallback<List<User>>() {
@Override
public void onSuccess(List<User> list1) {
countDownLatch.countDown();
list2.addAll(list1);
}
@Override
public void onFailure(Throwable throwable) {
countDownLatch.countDown();
logger.info("处理出错:", throwable);
}
});
}
countDownLatch.await(30, TimeUnit.MINUTES);
logger.info("符合条件的返回数据个数为:" + list2.size());
logger.info("回调函数:" + list2.toString());
long et = System.currentTimeMillis();
System.out.println(et - st);
} catch (Exception ex) {
ex.printStackTrace();
}
return "正在处理......";
}
}
package com.suning.sntcscase.controller.thread;
import com.suning.sntcscase.entity.User;
import com.suning.sntcscase.service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
public class ConCallable implements Callable {
private List<User> list;
public ConCallable(UserService userService, int end) {
this.userService = userService;
// this.begin = begin;
this.end = end;
}
private UserService userService;
private int begin;
private int end;
@Override
public Object call() throws Exception {
List<User> listRe = new ArrayList<>();
for(long i = end-250;i <end;i++){
User entity = new User();
entity.setAge(155);
entity.setId(i);
entity.setPassword("7777");
entity.setUserName("saasdsad");
// int xx = (int)i;
userService.update(entity);
listRe.add(entity);
}
return listRe;
}
public void setList(List<User> list) {
this.list = list;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2012-08-13 35hibernate_fetch_6_batch_size
2012-08-13 36hibernate_fetch_7_set_batch_size
2012-08-13 34hibernate_fetch_5_set_subselect
2012-08-13 33hibernate_fetch_4_set_join
2012-08-13 32hibernate_fetch_3_set_select
2012-08-13 31hibernate_fetch_2_join
2012-08-13 30hibernate_fetch_1_select