多线程Future+callable请求案例

多线程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);
	}
}

总结:比较原生,没有引入线程池,但是对项目“杀伤力较小,安全”,不会对原项目进行大改动,也便于排查。有需要的小伙伴可以自己动手试一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值