Java多线程开发中的Springboot 中异步请求方法的使用

SpringBoot 八种实现异步的方法 异步的八种实现方式 阅读详情

在过往的后台开发中,我们往往使用java自带的线程或线程池,来进行异步的调用。这对于效果来说没什么,甚至可以让开发人员对底层的状况更清晰,但是对于代码的易读性和可维护性却非常的差。

开发人员在实际使用过程中,应该更多的将精力放置在业务代码的书写过程中,而不是系统代码的维护中。你需要懂,但是不需要你直接维护去写,这才是编程语言的风向标。(这也是为什么spring在目前的java开发中,占用比重如此之大的原因之一)

下面来看使用Springboot 来实现异步调用的集中场景

一、简易注解,无需额外配置

1、添加@EnableAsync 到启动类(或者线程池配置类中)

2、添加@Async到需要异步执行的方法中

代码如下:

启动类

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">1 @EnableAsync
2 @SpringBootApplication
3 public class DemoLearnSpringbootApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(DemoLearnSpringbootApplication.class, args);
7     }
8 }

调用类

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class SimpleAsyncDemo {
 3     @Autowired
 4     private SimpleTaskHandler simpleTaskHandler;
 5 
 6 
 7     @PostConstruct
 8     public void execTaskHandler1() {
 9         try {
10             simpleTaskHandler.handle1(2);
11             simpleTaskHandler.handle2(2);
12             simpleTaskHandler.handle3(2);
13             simpleTaskHandler.handle1(2);
14             simpleTaskHandler.handle2(2);
15             simpleTaskHandler.handle3(2);
16             simpleTaskHandler.handle1(2);
17             simpleTaskHandler.handle2(2);
18             simpleTaskHandler.handle3(2);
19         } catch (InterruptedException e) {
20             e.printStackTrace();
21         }
22     }
23   
24 }

被异步调用的类

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class SimpleTaskHandler {
 3 
 4     public void printCurrentTime(String key) {
 5         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         System.out.println(format.format(new Date()) + "***" + key + "****" + Thread.currentThread().getName());
 7     }
 8 
 9     @Async
10     public void handle1(int time) throws InterruptedException {
11         TimeUnit.SECONDS.sleep(time);
12         printCurrentTime("handle1");
13     }
14 
15     @Async
16     public void handle2(int time) throws InterruptedException {
17         TimeUnit.SECONDS.sleep(time);
18         printCurrentTime("handle2");
19     }
20 
21     @Async
22     public void handle3(int time) throws InterruptedException {
23         TimeUnit.SECONDS.sleep(time);
24         printCurrentTime("handle3");
25     }
26 
27 
28 }

执行结果

handle1、handle2、handle3的执行结果为乱序,不可预估。这样最简易的通过2个注解即完成异步线程的调用了。

细心的同学已经发现了,连续调用9次异步线程后,最后一次的线程名称就会与之前的重复。这是由于默认的线程池配置的结果。

默认配置如下

<pre class="prettyprint hljs gradle" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"># 核心线程数
spring.task.execution.pool.core-size=8  
# 最大线程数
spring.task.execution.pool.max-size=16
# 空闲线程存活时间
spring.task.execution.pool.keep-alive=60s
# 是否允许核心线程超时
spring.task.execution.pool.allow-core-thread-timeout=true
# 线程队列数量
spring.task.execution.pool.queue-capacity=100
# 线程关闭等待
spring.task.execution.shutdown.await-termination=false
spring.task.execution.shutdown.await-termination-period=
# 线程名称前缀
spring.task.execution.thread-name-prefix=task-

二、自定义线程池

只通过注解来完成异步线程调用,简单明了,对应的异步线程来自springboot 默认生成的异步线程池。但是有些场景却并不满足。所以我们需要针对业务需要定义自己的线程池配置文件

1、在application.properties中定义我们自己的线程池配置

2、在springboot项目中,添加对应的线程池bean对象

3、添加@EnableAsync 到启动类(或者线程池配置类中)

4、添加@Async到需要异步执行的方法中

代码如下:

application.properties配置文件

<pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">task.pool.demo.corePoolSize= 5
task.pool.demo.maxPoolSize= 10
task.pool.demo.keepAliveSeconds= 300
task.pool.demo.queueCapacity= 50

调用类

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class SimpleAsyncDemo {
 3 
 4     @Autowired
 5     private PoolTaskHandler poolTaskHandler;
 6 
 7 
 8     @PostConstruct
 9     public void execTaskHandler2() {
10         try {
11             poolTaskHandler.handle1(2);
12             poolTaskHandler.handle2(2);
13             poolTaskHandler.handle3(2);
14             poolTaskHandler.handle1(2);
15             poolTaskHandler.handle2(2);
16             poolTaskHandler.handle3(2);
17             poolTaskHandler.handle1(2);
18             poolTaskHandler.handle2(2);
19             poolTaskHandler.handle3(2);
20         } catch (InterruptedException e) {
21             e.printStackTrace();
22         }
23     }
24 
25 }

异步线程池的配置类

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Configuration
 2 public class ThreadPoolConfig {
 3 
 4     @Value("${task.pool.demo.corePoolSize}")
 5     private int corePoolSize;
 6     @Value("${task.pool.demo.maxPoolSize}")
 7     private int maxPoolSize;
 8     @Value("${task.pool.demo.queueCapacity}")
 9     private int queueCapacity;
10     @Value("${task.pool.demo.keepAliveSeconds}")
11     private int keepAliveSeconds;
12 
13 
14     @Bean("handleAsync")
15     public TaskExecutor taskExecutor() {
16         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
17         // 设置核心线程数
18         executor.setCorePoolSize(corePoolSize);
19         // 设置最大线程数
20         executor.setMaxPoolSize(maxPoolSize);
21         // 设置队列容量
22         executor.setQueueCapacity(queueCapacity);
23         // 设置线程活跃时间(秒)
24         executor.setKeepAliveSeconds(keepAliveSeconds);
25         // 设置默认线程名称前缀
26         executor.setThreadNamePrefix("Thread-ABC-");
27         // 设置拒绝策略
28         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
29         // 等待所有任务结束后再关闭线程池
30         executor.setWaitForTasksToCompleteOnShutdown(true);
31         return executor;
32     }
33 }

被异步调用的类

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class PoolTaskHandler {
 3 
 4     public void printCurrentTime(String key) {
 5         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         System.out.println(format.format(new Date()) + "***" + key + "****" + Thread.currentThread().getName());
 7     }
 8 
 9     @Async("handleAsync")
10     public void handle1(int time) throws InterruptedException {
11         TimeUnit.SECONDS.sleep(time);
12         printCurrentTime("handle-1");
13     }
14 
15     @Async("handleAsync")
16     public void handle2(int time) throws InterruptedException {
17         TimeUnit.SECONDS.sleep(time);
18         printCurrentTime("handle-2");
19     }
20 
21     @Async("handleAsync")
22     public void handle3(int time) throws InterruptedException {
23         TimeUnit.SECONDS.sleep(time);
24         printCurrentTime("handle-3");
25     }
26 
27 
28 }

执行结果如下

与上例类似,我们发现请求线程变成了每5个一批,这与我们在配置文件中的配置互相印证

调用类

<pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class SimpleAsyncDemo {
 3 
 4     @Autowired
 5     private ReturnTaskHandler returnTaskHandler;
 6 
 7     @PostConstruct
 8     public void execTaskHandler3() {
 9         try {
10             String a1 = returnTaskHandler.handle1(2);
11             String a2 = returnTaskHandler.handle2(2);
12             String a3 = returnTaskHandler.handle3(2);
13             String a4 = returnTaskHandler.handle1(2);
14             String a5 = returnTaskHandler.handle2(2);
15             String a6 = returnTaskHandler.handle3(2);
16             String a7 = returnTaskHandler.handle1(2);
17             String a8 = returnTaskHandler.handle2(2);
18             String a9 = returnTaskHandler.handle3(2);
19             int c = 1;
20         } catch (InterruptedException e) {
21             e.printStackTrace();
22         }
23     }
24 
25 }

被调用类

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class ReturnTaskHandler {
 3 
 4     public void printCurrentTime(String key) {
 5         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         System.out.println(format.format(new Date()) + "***" + key + "****" + Thread.currentThread().getName());
 7     }
 8 
 9     @Async("handleAsync")
10     public String handle1(int time) throws InterruptedException {
11         TimeUnit.SECONDS.sleep(time);
12         printCurrentTime("handle-1");
13         return "result1";
14     }
15 
16     @Async("handleAsync")
17     public String handle2(int time) throws InterruptedException {
18         TimeUnit.SECONDS.sleep(time);
19         printCurrentTime("handle-2");
20         return "result2";
21     }
22 
23     @Async("handleAsync")
24     public String handle3(int time) throws InterruptedException {
25         TimeUnit.SECONDS.sleep(time);
26         printCurrentTime("handle-3");
27         return "result3";
28     }
29 
30 }

其余代码继续我们使用上文中的其他代码

结果如下

所有结果返回都是null值。

如果想要拿到正确的执行结果,我们需要使用future接口类看来帮忙接住异步线程的返回结果(关于future等接口类的内容我会在后边的文章中讲解)

其余代码继续我们使用上文中的其他代码,改动的代码如下:

被调用类

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class ReturnSuccTaskHandler {
 3 
 4     public void printCurrentTime(String key) {
 5         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         System.out.println(format.format(new Date()) + "***" + key + "****" + Thread.currentThread().getName());
 7     }
 8 
 9     @Async("handleAsync")
10     public Future<String> handle1(int time) throws InterruptedException {
11         TimeUnit.SECONDS.sleep(time);
12         printCurrentTime("handle-1");
13         return new AsyncResult<>("result1");
14     }
15 
16     @Async("handleAsync")
17     public Future<String> handle2(int time) throws InterruptedException {
18         TimeUnit.SECONDS.sleep(time);
19         printCurrentTime("handle-2");
20         return new AsyncResult<>("result2");
21     }
22 
23     @Async("handleAsync")
24     public Future<String> handle3(int time) throws InterruptedException {
25         TimeUnit.SECONDS.sleep(time);
26         printCurrentTime("handle-3");
27         return new AsyncResult<>("result3");
28     }
29 
30 
31 }

调用类

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"> 1 @Component
 2 public class SimpleAsyncDemo {
 3 
 4 
 5     @Autowired
 6     private ReturnSuccTaskHandler returnSuccTaskHandler;
 7 
 8 
 9 
10     @PostConstruct
11     public void execTaskHandler4() {
12         try {
13             Future<String> a1 = returnSuccTaskHandler.handle1(2);
14             Future<String> a2 = returnSuccTaskHandler.handle2(2);
15             Future<String> a3 = returnSuccTaskHandler.handle3(2);
16             Future<String> a4 = returnSuccTaskHandler.handle1(2);
17             Future<String> a5 = returnSuccTaskHandler.handle2(2);
18             Future<String> a6 = returnSuccTaskHandler.handle3(2);
19             Future<String> a7 = returnSuccTaskHandler.handle1(2);
20             Future<String> a8 = returnSuccTaskHandler.handle2(2);
21             Future<String> a9 = returnSuccTaskHandler.handle3(2);
22             while (true){
23                 // 如果任务都做完就执行如下逻辑
24                 if (a1.isDone() &&
25                         a2.isDone()&&
26                         a3.isDone()&&
27                         a4.isDone()&&
28                         a5.isDone()&&
29                         a6.isDone()&&
30                         a7.isDone()&&
31                         a8.isDone()&&
32                         a9.isDone()){
33                     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
34                     System.out.println(format.format(new Date()) + "async task end.");
35                     System.out.println("async result:"+a1.get());
36                     System.out.println("async result:"+a2.get());
37                     System.out.println("async result:"+a3.get());
38                     System.out.println("async result:"+a3.get());
39                     System.out.println("async result:"+a4.get());
40                     System.out.println("async result:"+a5.get());
41                     System.out.println("async result:"+a6.get());
42                     System.out.println("async result:"+a7.get());
43                     System.out.println("async result:"+a8.get());
44                     System.out.println("async result:"+a9.get());
45                     break;
46                 }
47             }
48         } catch (InterruptedException | ExecutionException e) {
49             e.printStackTrace();
50         }
51     }
52 
53 
54 }

输出结果如下,我们可以发现 ,1、可以拿到返回结果,2、在最后一个子任务执行完成后,即立刻拿到结果。

Spring Boot异步请求处理框架 ​ 在Spring Boot项目中,经常会遇到处理时间过长,导致出现HTTP请求超时问题,状态码:502。​ 例如一个文件导入接口需要导入一个Excel文件的学员记录,原来是针对一个班的学员,最多500条记录,1分钟的HTTP超时时长之内基本可以响应。现在将很多班级的学员混在一起,放在一个Excel文件中(这样可以提高操作人员的工作效率),比如5万条学员记录,于是就出现HTTP请求超时问题。​ ​解决方案有:1)Ajax异步请求方式;2)WebSocket方式;3)异步请求处理方式:请求+轮询。 阅读详情

相关推荐

配置@Async异步任务的线程池

上一篇我们介绍了如何使用@Async注解来创建异步任务,我可以用这种方法来实现一些并发操作,以加速任务的执行效率。但是,如果只是如前文那样直接简单的创建来使用,可能还是会碰到一些问题。存在有什么问题呢?先来思考下,下面的这个接口,通过异步任务加速执行的实现,是否存在问题或风险呢? @RestController public class HelloController { @Autowired private AsyncTasks asyncTasks; @G

q1472750149的博客 1349

java中的异步请求

线程池 多线程

qq_44718662的博客 2471

Java多线程开发系列之五:Springboot 中异步请求方法的使用

开发人员在实际使用过程中,应该更多的将精力放置在业务代码的书写过程中,而不是系统代码的维护中。在过往的后台开发中,我们往往使用java自带的线程或线程池,来进行异步的调用。只通过注解来完成异步线程调用,简单明了,对应的异步线程来自springboot 默认生成的异步线程池。如果想要拿到正确的执行结果,我们需要使用future接口类看来帮忙接住异步线程的返回结果(关于future等接口类的内容我会在后边的文章中讲解)细心的同学已经发现了,连续调用9次异步线程后,最后一次的线程名称就会与之前的重复。...

lt_xiaodou的博客 226

SpringBoot 8种异步实现方式

什么是异步?在同步操作中,执行到发送短信的时候,我们必须等待这个方法彻底执行完才能执行赠送积分这个操作,如果赠送积分这个动作执行时间较长,发送短信需要等待,这就是典型的同步场景。实际上,发送短信和赠送积分没有任何的依赖关系,通过异步,我们可以实现赠送积分和发送短信这两个操作能够同时进行,比如:这就是所谓的异步,是不是非常简单,下面就说说异步的几种实现方式吧。/**

qq_21305943的专栏 6608

【Java】SpringBoot中使用ThreadPoolTaskExecutor实现异步多线程

一、配置类 @Configuration public class TaskConfig { @Bean("taskModuleExecutor") public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolT...

Mr_EvanChen的Blog 3828

Springboot中开启多线程,实现异步非阻塞、异步阻塞、有无返回值的场景

开启多线程异步的方式有很多种,不单单局限以上方式,感兴趣的可以自行研究测试下,比如还可以使用来开启多线程,然后分别使用对应的execute()和submit()方法实现无返回值和有返回值的效果;以上内容均为个人理解,如存在不当欢迎提出改进。

weixin_45151960的博客 4092

Java多线程开发系列之Springboot 中异步请求方法的使用

在过往的后台开发中,我们往往使用java自带的线程或线程池,来进行异步的调用。这对于效果来说没什么,甚至可以让开发人员对底层的状况更清晰,但是对于代码的易读性和可维护性却非常的差...

guanshengg的博客 2118

SpringBoot多线程在项目中的实际应用

1、项目中实际运用---批量执行异步任务 2、批量异步查询-CompletableFuture的使用 2.1 几种创建方式 2.2 线程执行完成后走的方法 2.3 线程执行结果转换 2.3.1 thenApply 2.3.2 thenCompose 2.4 线程执行结果消费 2.4.1 thenAccept 2.4.2 thenAcceptBoth 2.4.3 thenRun 2.5 线程执行结果组合 3、项目中实际运用---多线程批量查询 4、SpringBoot中使用多线程

张刚的博客 1349

SpringBoot使用多线程

一、概述 1、为什么使用多线程 在我们开发系统过程中,经常会处理一些好费时间的任务(如:向数据库中插入上百万数据,将会导致系统等待),这个时候就会自然想到使用多线程。 2、为什么使用Spring来实现多线程 使用Spring比使用JDK原生的并发API更简单。(@Async就能解决)。 一般的开发环境都会集成Spring框架,Bean也都交给Spring来管理,因此,Spring实现多线程更简单。 3、为什么需要使用异步 传统的调用方式:调用一个服务,需要等待服务调用完成后,才能执行后面的代码,

csdn_life18的博客 3万+

【SpringBoot深入浅出系列】SpringBoot之多线程

多线程编程是提升 Spring Boot 应用性能和响应速度的重要手段。通过本文的介绍,我们了解了多线程的基本概念、Spring Boot 中的多线程应用场景、实现方式以及常见问题的解决方案。在实际开发中,我们需要根据具体的业务需求和场景,合理运用多线程技术,以实现高效、稳定的应用程序。希望本文能为读者在 Spring Boot 多线程编程方面提供有益的参考和帮助,让大家能够更好地掌握和应用这一强大的技术。

邓邓子的博客 1608

别再瞎调参数了!用MATLAB fmincon优化傅里叶级数,为你的机器人设计“完美”激励轨迹

本文详细介绍了如何利用MATLAB的fmincon优化器设计机器人激励轨迹,通过优化傅里叶级数参数,解决传统试错法的低效问题。文章涵盖了目标函数设计、约束条件转化及优化算法选择,帮助工程师实现高精度的系统辨识和动力学参数优化。

weixin_42613501的博客 240

SpringBoot开启异步多线程

SpringBoot 的异步多线程需要从 java 的多线程基础说起,可以参考java 多线程实现的三种方式区别。SpringBoot 在此基础上进行了多次封装,所以使用起来非常方便。

清泉影月 6349

生信实战:用bcftools玩转VCF文件处理(从压缩到注释全流程)

本文详细介绍了使用bcftools处理VCF文件的全流程实战指南。从数据准备、压缩索引、信息提取、质量统计到核心的变异过滤与功能注释,手把手教你高效管理基因组变异数据。重点阐述了如何利用bcftools进行VCF文件注释,整合外部数据库信息,为变异位点添加关键的生物学标签,是生物信息学数据分析的必备技能。

rgv23456789的博客 341

springboot中多线程的使用

程序:是为完成特定任务,用某种语言编写的一组指令的集合,即指一段静态的代码,静态对象。进程:是程序的一次执行过程,或是正在运行的一个程序,是一个动态的过程,有它自身的产生,存在和消亡的过程。-------生命周期线程:进程可进一步细化为线程,是一个程序内部的一条执行路径。

qq_36996785的博客 3846

安卓端目标检测模型部署血泪史:从NCNN/TFLite/MNN全军覆没到Torchscipts终见光明

公司需在Android设备部署目标检测模型,要求: ✅ 实时检测速度≥15FPS ✅ 模型精度损失<5% ✅ 兼容中低端安卓设备👉 GitHub项目地址:pytorch/android-demo-app/ObjectDetection我已上传这份源码到csdn下载准备模型(必须走这步才能跑通安卓端源码)如果您没有设置PyTorch环境来运行该脚本,可以下载模型文件此处转到“android演示app/ObjectDetection/app/src/main/assets”文件夹,然后跳过此步骤的其余部分,直

机器学习、深度学习、强化学习、迁移学习 1567

MaxDEA如何计算超效率DEA

MaxDEA如何计算超效率DEA第一步,导入数据,设置输出和输出,点击OK;第二步,运行模型; 本文计算的是超效率DEA-BCC。 第一步,导入数据,设置输出和输出,点击OK; 第二步,运行模型; 在distance选项框中选择Radial:CCR(1978);BCC(1984)。 在Orientation中选择Non-oriented。其中,Input-oriented是投入最小;Outp...

王樰沫 1万+

JAVA SpringBoot 多线程配置、使用方法及注意事项

在Spring Boot中配置和使用多线程通常涉及以下几个方面:配置线程池、使用@Async注解、处理异步方法的返回值和异常、以及遵循最佳实践。

技术分享、程序员趣事、行业趋势等内容! 1649

嵌入式开发新思路:用QP框架5步搞定状态机设计(附STM32移植指南)

本文介绍了在嵌入式开发中使用QP框架进行状态机设计的新思路。通过对比传统RTOS与事件驱动模型的差异,详细解析了QP框架的核心组件与优势,并提供了基于STM32的5步实战指南,包括环境搭建、状态机实现与性能优化技巧,帮助开发者从顺序阻塞思维转向高效的事件驱动架构。

milk5的博客 900

SpringBoot多线程进行异步请求的处理

近期在协会博客园中,有人发布了博客,系统进行查重的时候需要十秒左右时间才能处理完,由于一开始是单例模式,导致,在某人查重的时候整个系统是不会再响应别的请求的,导致了系统假死状态,那么我们就应该使用多线程进行处理,将这种不能快速返回结果的方法交给线程池进行处理。...

HumorChen的博客 3120

为什么你的SiC MOSFET仿真不准?可能是离子注入的‘沟道效应’在捣鬼(Silvaco BCA模块详解)

本文深入分析了SiC MOSFET仿真中离子注入沟道效应的关键影响,揭示了Silvaco BCA模块在模拟SiC晶向和角度参数时的核心设置要点。通过详细解析晶圆偏切与注入角度的等效关系,提供优化MOSFET沟道区注入仿真的五步实战方法,帮助工程师将阈值电压仿真误差从26%降低到5%以内。

weixin_26789847的博客 410

基于THUCNews数据集bert bert_wwm的新闻文本分类

基于THUCNews数据集bert bert_wwm的新闻文本分类

984

springboot+多线程

在springboot的过程中,我们会使用到多线程去同时执行某个任务,那么我们就需要多线程,在以前的java中是自己去实现Thread或者runnable来实现多线程,springboot中已经自己拥有了多线程的注解@Async,那么我们现在使用这个注解来实现springboot的多线程 1.创建多线程的配置信息类: 在这个类中需要注解为@Configuration 及@Bean("XXX"),其中@Bean中的名称在我们日后使用@Async注解时需要制定的Bean类名,具体代码如下: pack.

jia.huang的博客 4146

〖Python WEB 自动化测试实战篇⑤〗- selenium 元素定位详解 - (八大元素定位方式)

测试对象的定位和操作是我们利用 selenium 编写自动化脚本和 webdriver 的核心内容,其中 "操作" 这一部分又是建立在 "selenium" 元素定位的基础之上的。所以对元素对象的定位就显得越发的重要,接下来我们就学习一下常用的元素定位方法有哪些。......

易编橙 · 终身成长社群,相遇已是上上签! 4万+

SpringBoot中使用多线程调用异步方法,异步方法有无返回值例子。

快速了解@Async注解的用法,包括异步方法无返回值、有返回值,最后总结@Async注解失效的几个坑。在我们的 SpringBoot 应用中,经常会遇到在一个接口中,同时做事情1,事情2,事情3,如果同步执行的话,则本次接口时间取决于事情1 2 3执行时间之和;如果三件事同时执行的话,那么本次接口的运行时间取决于事情1 2 3执行时间最长的那个。合理的使用多线程,可以大大缩短接口的运行时间。几个例子简单的介绍了一下多线程如何调用异步方法的例子。

L 755

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

SpringBoot多线程

SpringBoot开启异步多线程_springboot 异步线程-优快云博客这个就是推荐用法了在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法_java async调用了两次-优快云博客了解了失效的原因,解决的方法就简单了(两种):1.把这两个方法分开到不同的类中;2.把注解加到类名上面(这个类所有方法may都有注解的效果了..);

qq_52122048的博客 2846

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

上一篇: 怎样去优化JAVA中的实现代码
下一篇: Spring Security 自定义授权服务器实践
Java_ttcd
博客等级 码龄4年 198粉丝 190原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值