JAVA多线程提高接口响应速度实例

package com.ctx.controller;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctx.bean.Achievement;
import com.ctx.bean.Student;
import com.ctx.service.StudentService;

@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;
	}

	//多线程并行执行
		@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;
		}
}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值