多线程Future+callable请求案例
想要实现主线程同步,子线程异步调用。因为主线程需要同步,可以选择
1、线程池的线程池 submit方法 (需要了解submit和execute区别)
2、Future的回调实现
考虑到项目的简单处理,不给项目增加难度,选择了第二种,直接上代码。
一、首先 contorller,路由和主线程入口
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequestMapping("studyContorller")
public class studyContorller{
@Autowired
AsyncaService asyncService;
@ResponseBody
@RequestMapping("studyFuture")
public Object studyFuture() throws ExecutionException, InterruptedException {
//vo :替换成你自己最终实现返回的对象
List<Future<List<VO>>> futureList=new LinkedList<>();
//开启子线程
for(Map<String ,Object> temp : mapList ){
Future<List<VO>>tempList= asyncService.method(temp);
}
//主线程
for( Future< List<VO> >temp: futureList){
resultList.addAll(temp.get());
}
return resultList;
}
}
二、抽象接口 AsyncaService
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@Service
public interface AsyncaService {
Future< List<VO>> method(Map<String,Object> map);
}
三、抽象接口 AsyncServiceImpl
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncaService {
@Autowired
private ObjService objService ;
@Async
@Override
public Future< List<Vo>> method(Map<String, Object> map) {
TaskVO taskVo=new TaskVO (objService ,map);
List<VO> call = null;
try {
call = taskVo.call();
} catch (Exception e) {
e.printStackTrace();
}
return AsyncResult.forValue(call);
}
}
三、抽象接口 TaskVo
import java.util.Map;
import java.util.concurrent.Callable;
@Slf4j
public class TaskVo implements Callable<List<Vo> >{
private ObjService objService ;
private final Map<String, Object> map;
public AttendanceTask( ObjService objService ,Map<String, Object> map) {
this.objService =objService;
this.map=map;
}
@Override
public List<Vo> call() throws Exception {
//进行业务查库数据返回
return objService.method(map);
}
}
总结:比较原生,没有引入线程池,但是对项目“杀伤力较小,安全”,不会对原项目进行大改动,也便于排查。有需要的小伙伴可以自己动手试一下。