高并发下接口同时处理多业务请求。提高接口响应开启 Callable 多线程实现异步请求

      Callable两种实现方法 第二种方式可读性高。

适用的场景  :查询接口、包括多个独立的业务逻辑查询数据、最后数据统一组装

可以对比下来 单线程和多线程实现方式
 
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
 
    //单线程串行执行
    @GetMapping("/studentInfo")
    public String studentInfo(Long no) {
        //开始时间
        Long startTime = System.currentTimeMillis();
        //查询学生成绩
        Achievement achievement = studentService.findAchievementByNo(no);
        //查询学生基本信息
        Student student = studentService.findStudentByNo(no);
        String achievementStr = JSON.toJSONString(achievement);
        String studentStr = JSON.toJSONString(student);
        JSONObject achievementJson = JSONObject.parseObject(achievementStr);
        JSONObject studentJson = JSONObject.parseObject(studentStr);
        
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(achievementJson);
        jsonObject.putAll(studentJson);
        String result = jsonObject.toString();
        //结束时间
        Long endTime = System.currentTimeMillis();
        Long diffTime=endTime-startTime;
        System.out.println("代码执行时间:"+diffTime);
        return result;
    }
 
    //  多线程并行执行 写法实现方式2
        @GetMapping("/studentInfo2")
        public String studentInfo2(Long no) {
            //开始时间
            Long startTime = System.currentTimeMillis();
            //学生成绩
            Callable<JSONObject> achievementJSON = new Callable<JSONObject>() {
                @Override
                public JSONObject call() throws Exception {
                    Achievement achievement = studentService.findAchievementByNo(no);
                    String achievementStr = JSON.toJSONString(achievement);
                    JSONObject achievementJson = JSONObject.parseObject(achievementStr);
                    return achievementJson;
                    //return null;
                }
            };
            //学生基本信息
            Callable<JSONObject> studentJSON = new Callable<JSONObject>() {
                @Override
                public JSONObject call() throws Exception {
                    //查询学生基本信息
                    Student student = studentService.findStudentByNo(no);
                    String studentStr = JSON.toJSONString(student);
                    JSONObject studentJson = JSONObject.parseObject(studentStr);
                    return studentJson;
                }
            };
            FutureTask<JSONObject> achievementFutureTask = new FutureTask<JSONObject>(achievementJSON);
            FutureTask<JSONObject> studentFutureTask = new FutureTask<JSONObject>(studentJSON);
            //执行线程
            new Thread(achievementFutureTask).start();
            new Thread(studentFutureTask).start();
            
            JSONObject jsonObject = new JSONObject();
            try {
                //get()阻塞监听call()方法的返回值。
                jsonObject.putAll(achievementFutureTask.get());
                jsonObject.putAll(studentFutureTask.get());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            String result = jsonObject.toString();
            //结束时间
            Long endTime = System.currentTimeMillis();
            Long diffTime=endTime-startTime;
            System.out.println("代码执行时间:"+diffTime);
            return result;
        }

 

 

//  多线程并行执行  写法实现方式2
        @GetMapping("/studentInfo2")
        public String studentInfo2(Long no) {
            //开始时间
            Long startTime = System.currentTimeMillis();
            //学生成绩
            Callable<JSONObject> achievementJSON = () -> {
            return achievementCall();
            };

          //学生基本信息
            Callable<JSONObject> studentJSON =() -> {
            return studentCall();
            };
            FutureTask<JSONObject> achievementFutureTask = new FutureTask<JSONObject>(achievementJSON);
            FutureTask<JSONObject> studentFutureTask = new FutureTask<JSONObject>(studentJSON);
            //执行线程
            new Thread(achievementFutureTask).start();
            new Thread(studentFutureTask).start();
            
            JSONObject jsonObject = new JSONObject();
            try {
                //get()阻塞监听call()方法的返回值。
                jsonObject.putAll(achievementFutureTask.get());
                jsonObject.putAll(studentFutureTask.get());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            String result = jsonObject.toString();
            //结束时间
            Long endTime = System.currentTimeMillis();
            Long diffTime=endTime-startTime;
            System.out.println("代码执行时间:"+diffTime);
            return result;
        }

 

 public JSONObject achievementCall() throws Exception {
                    Achievement achievement = studentService.findAchievementByNo(no);
                    String achievementStr = JSON.toJSONString(achievement);
                    JSONObject achievementJson = JSONObject.parseObject(achievementStr);
                    return achievementJson;
                    //return null;
                }

 

 public JSONObject studentCall() throws Exception {
                    //查询学生基本信息
                    Student student = studentService.findStudentByNo(no);
                    String studentStr = JSON.toJSONString(student);
                    JSONObject studentJson = JSONObject.parseObject(studentStr);
                    return studentJson;
                }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值