同时跑list
@Slf4j
@Component
public class Test {
@Autowired
private XXXMapper xxxMapper;
private static Test layoutNumUtils;
@PostConstruct
public void init(){
layoutNumUtils = this;
layoutNumUtils.xxxMapper = this.xxxMapper;
}
public synchronized void handleList(List<Info> data, int threadNum) {
int length = data.size();
if (length<threadNum){
log.info("当前线程数大于总数,默认使用总数数量的线程数");
threadNum = length;
}
if (length == 0){
log.info("当前无需运行的数据,默认使用1个线程数");
threadNum = 1;
}
int tl = length % threadNum == 0 ? length / threadNum : (length
/ threadNum + 1);
for (int i = 0; i < threadNum; i++) {
int end = (i + 1) * tl;
HandleThread thread = new HandleThread("线程[" + (i + 1) + "] ", data, i * tl, end > length ? length : end);
thread.start();
}
}
class HandleThread extends Thread {
private String threadName;
private List<DemoInfo> data;
private int start;
private int end;
@Autowired
private ServiceImpl service;
public HandleThread(String threadName, List<DemoInfo> data, int start, int end) {
this.threadName = threadName;
this.data = data;
this.start = start;
this.end = end;
}
@Override
public void run() {
List<Info> subList = data.subList(start, end);
System.out.println(threadName+"处理了"+subList.size()+"条!");
}
}