当前位置:   article > 正文

赶紧收藏-针对SpringBoot的七个优化_csdn springboot 提高代码质量

csdn springboot 提高代码质量

本文是向大家介绍常用框架springboot的常用优化,能够提高模块的吞吐并发。


目录

  1. 异步执行

  1. 增加内嵌 Tomcat 的最大连接数

  1. 使用 @ComponentScan()

  1. 默认 Tomcat 容器改为 Undertow

  1. 使用 BufferedWriter 进行缓冲

  1. Deferred 方式实现异步调用

  1. 异步调用可以使用 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

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/383270?site
推荐阅读
相关标签
  

闽ICP备14008679号