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

 

    原文作者:身前一尺是我的世界
    原文地址: https://blog.csdn.net/qq_39706570/article/details/107994130
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞