赞
踩
本文是向大家介绍常用框架springboot的常用优化,能够提高模块的吞吐并发。
目录
异步执行
增加内嵌 Tomcat 的最大连接数
使用 @ComponentScan()
默认 Tomcat 容器改为 Undertow
使用 BufferedWriter 进行缓冲
Deferred 方式实现异步调用
异步调用可以使用 AsyncHandlerInterceptor 进行拦截
实现方式二种:
使用异步注解 @aysnc、启动类:添加 @EnableAsync 注解
JDK 8 本身有一个非常好用的 Future 类——CompletableFuture
@AllArgsConstructor
public class AskThread implements Runnable{
private CompletableFuture<Integer> re = null;
public void run() {
int myRe = 0;
try {
myRe = re.get() * re.get();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(myRe);
}
public static void main(String[] args) throws InterruptedException {
final CompletableFuture<Integer> future = new CompletableFuture<>();
new Thread(new AskThread(future)).start();
//模拟长时间的计算过程
Thread.sleep(1000);
//告知完成结果
future.complete(60);
}
}
在该示例中,启动一个线程,此时 AskThread 对象还没有拿到它需要的数据,执行到 myRe = re.get() * re.get() 会阻塞。
我们用休眠 1 秒来模拟一个长时间的计算过程,并将计算结果告诉 future 执行结果,AskThread 线程将会继续执行。
public class Calc {
public static Integer calc(Integer par
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。