增加使用副线程处理请求的接口
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
package com.micro.fast.security.demo.controller;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/logger")
|
||||
public class AsyncController {
|
||||
public static final Logger logger = LoggerFactory.getLogger(AsyncController.class);
|
||||
|
||||
/**
|
||||
* 使用Callable创建副线程来处理请求
|
||||
* tomcat中主线程的数量是有限的,使用副线程处理请求虽然请求消耗的总时间不变,但是主线程结束的快,能继续去处理其他请求能
|
||||
* 提高服务起的吞吐量
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/callable")
|
||||
public Callable<String> order(){
|
||||
logger.info("主线程开始");
|
||||
Callable<String> result = new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
logger.info("副线程开始了");
|
||||
Thread.sleep(1000);
|
||||
logger.info("副线程结束了");
|
||||
return "success";
|
||||
}
|
||||
};
|
||||
logger.info("主线程结束");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user