免费做简单网站,全球速卖通官网,开发一个app多少钱,深圳网页设计网站制作大家好#xff0c;我是程序员大猩猩。
大家都知道#xff0c;在我们实际开发过程中#xff0c;我们经常会遇到一些耗时的业务和逻辑#xff0c;比如说要上传什么大文件#xff0c;又或者是大文件的数据处理。我们不能一个接口上等着这些耗时任务完成之后了#xff0c;再…大家好我是程序员大猩猩。
大家都知道在我们实际开发过程中我们经常会遇到一些耗时的业务和逻辑比如说要上传什么大文件又或者是大文件的数据处理。我们不能一个接口上等着这些耗时任务完成之后了再返回那用户体验度会大打折扣的。
这时候我们最基本的操作就是使用多线程处理或者是异步线程处理。这里我们说一下异步线程处理。
那么我们来说一下微服务中如何使用异步线程呢
一、使用 Async 注解我们来看看它的源码
Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface Async {/*** A qualifier value for the specified asynchronous operation(s).* pMay be used to determine the target executor to be used when executing* the asynchronous operation(s), matching the qualifier value (or the bean* name) of a specific {link java.util.concurrent.Executor Executor} or* {link org.springframework.core.task.TaskExecutor TaskExecutor}* bean definition.* pWhen specified on a class-level {code Async} annotation, indicates that the* given executor should be used for all methods within the class. Method-level use* of {code Async#value} always overrides any value set at the class level.* since 3.1.2*/String value() default ;
}
我们看到Async内只有一个参数value这个value的设置来确定异步线程指定线程池的名字。
当然这里我们可以不设置这个value可以使用默认的。但是为什么要设置这个value,来指定线程池呢因为指定线程是为了控制和管理异步任务。
a. 如果不指定线程池Spring 默认使用 SimpleAsyncTaskExecutor这不是一个真正的线程池因为它为每个任务创建一个新的线程。这可能导致线程数量的快速增长从而消耗大量系统资源。通过指定一个真正的线程池如 ThreadPoolTaskExecutor可以复用线程减少资源消耗。b. 线程池可以提供更好的性能因为它可以减少线程创建和销毁的开销。线程池中的线程可以被重复使用而不是每次执行异步任务时都创建新的线程。
c. 通过为不同的服务或组件指定不同的线程池可以实现线程隔离。这意味着如果一个服务出现异常或者需要大量时间来处理任务它不会影响到其他服务的性能。d. 线程池提供了任务调度和管理的能力比如设置核心线程数、最大线程数、队列容量等这样可以更精细地控制任务的执行行为。e. 线程池通常提供了任务执行错误的处理机制比如当任务执行失败时的重试策略。f. 线程池可以提供线程的运行状态和性能指标这对于监控和调试应用程序是非常有用的。怎么设置线程池
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;Configuration
public class ThreadPoolConfig {Bean(name taskExecutor)public ThreadPoolTaskExecutor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(2); // 核心线程数executor.setMaxPoolSize(5); // 最大线程数executor.setQueueCapacity(10); // 队列容量executor.setThreadNamePrefix(Async-); // 线程名称前缀executor.initialize();return executor;}
}
设置好线程池后我们就可以在方法上使用Async开启异步线程了。
Async(taskExecutor)
public void executeAsyncTask() {// 异步任务逻辑
}
最后要记住在微服启动类之上加入注释EnableAsync就可以了。
SpringBootApplication
EnableAsync
public class AsyncApplication {public static void main(String[] args) {SpringApplication.run(AsyncApplication.class, args);}
}
二、使用 CompletableFuture
CompletableFuture 是 Java 8 引入的一个类用于表示异步计算的结果。通过 CompletableFuture我们可以很方便地实现异步操作并且可以链式调用多个异步任务。
Service
public class CompletableFutureService {public CompletableFutureString executeAsyncTask() {return CompletableFuture.supplyAsync(() - {System.out.println(执行异步任务 Thread.currentThread().getName());return 异步任务执行结果;});}
}RestController
public class CompletableFutureController {Autowiredprivate CompletableFutureService completableFutureService;GetMapping(/completableFuture)public CompletableFutureString completableFuture() {return completableFutureService.executeAsyncTask();}
}三、组合使用 Async 和 Future
我们可以通过 Async 返回一个 Future 对象以便在需要时获取异步任务的执行结果。
Service
public class FutureService {Asyncpublic FutureString executeAsyncTask() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(执行异步任务 Thread.currentThread().getName());return new AsyncResult(异步任务执行结果);}
}RestController
public class FutureController {Autowiredprivate FutureService futureService;GetMapping(/future)public String future() throws ExecutionException, InterruptedException {FutureString future futureService.executeAsyncTask();return 异步任务执行结果 future.get();}
}
本文介绍了在SpringBoot中使用异步线程的三种方式使用Async注解、CompletableFuture和Async结合Future。在实际开发中我们可以根据具体需求选择合适的异步实现方式提高应用程序的性能和用户体验。