本章为介绍如何使用带有返回值的多线程进行业务处理,提高接口效率
源码:
版本一:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ListWithMutiThread {
public void dealListWithMutiThread(){
List<Object> list = new ArrayList<Object>(10000);
int index = 0;
ExecutorService ex = Executors.newFixedThreadPool(5);
int dealSize = 2000;
List<Future<List<Object>>> futures = new ArrayList<>(5);
//分配
for(int i=0;i<5;i++,index+=dealSize){
int start = index;
if(start>=list.size()) break;
int end = start + dealSize;
end = end>list.size() ? list.size() : end;
futures.add(ex.submit(new Task(list,start,end)));
}
try {
//处理
List<Object> result = new ArrayList<>();
for(Future<List<Object>> future : futures){
//合并操作
result.addAll(future.get());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class Task implements Callable<List<Object>> {
private List<Object> list;
private int start;
private int end;
public Task(List<Object> list,int start,int end){
this.list = list;
this.start = start;
this.end = end;
}
@Override
public List<Object> call() throws Exception {
Object obj = null;
List<Object> retList = new ArrayList<Object>();
for(int i=start;i<end;i++){
obj = list.get(i);
//你的处理逻辑
}
//返回处理结果
return retList;
}
}
}
版本二:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class ServalMethod {
/**
* 有返回值的线程
*/
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("------程序开始运行------");
Date date = new Date();
int taskSize = 5;
//创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
//创建多个有返回值的任务
List<Future> futures = new ArrayList<Future>();
for(int i=0;i<taskSize;i++){
Callable c = new MyCallable(i+"");
//执行线程并获取Future对象
Future f = pool.submit(c);
// System.out.println(">>>"+f.get().toString());
futures.add(f);
}
//关闭线程池
pool.shutdown();
//获取所有并发任务的运行结果
for(Future f:futures){
//从Future对象上获取任务的返回值,并输出到控制台
System.out.println(">>>"+f.get().toString());
}
Date date1 = new Date();
System.out.println("-----程序运行结束------,程序运行时间【"+(date1.getTime()-date.getTime())+"毫秒】");
}
}
class MyCallable implements Callable<Object>{
private String taskNum;
MyCallable(String taskNum){
this.taskNum=taskNum;
}
@Override
public Object call() throws Exception {
System.out.println(">>>"+taskNum+"任务启动");
Date dateTmpl = new Date();
Thread.sleep(1000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime()-dateTmpl.getTime();
System.out.println(">>>"+taskNum+"任务终止");
return taskNum+"任务返回运行结果【"+time+"ms】";
}
}
版本三:
//创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(yGood.size());
try
{
//创建多个有返回值的任务
List<Future<List<BigDecimal>>> futures = new ArrayList<Future<List<BigDecimal>>>();
if(!CollectionUtils.isEmpty(yGood)){
for (String good : yGood) {
Callable<List<BigDecimal>> c = new Callable<List<BigDecimal>>() {
@Override
public List<BigDecimal> call() throws Exception {
List<BigDecimal> weightArr = new ArrayList<>();
for (String date : xDate) {
BigDecimal weightDecimal = orderInfoMapper.countOrderWeight(45, date, good);
weightArr.add(weightDecimal != null ? weightDecimal.multiply(new BigDecimal("1000")) : new BigDecimal("0"));
}
return weightArr;
}
};
//执行线程并获取Future对象
Future<List<BigDecimal>> f = pool.submit(c);
futures.add(f);
}
//获取所有并发任务的运行结果
for(Future<List<BigDecimal>> f:futures){
//从Future对象上获取任务的返回值,并输出到控制台
goodsNums.add(f.get());
}
}
}catch(Exception ex)
{
Thread.currentThread().interrupt();
}finally{
pool.shutdown();
}