当前位置:首页 >> 中医丰胸 >> Java异步编程(5种异步借助方式详解)

Java异步编程(5种异步借助方式详解)

发布时间:2023-04-16

System.out.println("===task start==="); Thread.sleep(5000); System.out.println("===task finish==="); return 3; } }); //这里必需返回值时会堵塞主虚拟机,如果不必需返回值适用是OK的。扯也还能接收 //Integer result=future.get(); System.out.println("main函数督导之前"); System.in.read(); }三、CompletableFuture异步

Future 类通过 get() 原理堵塞继续前进获取异步督导的行驶结果,精度比较差。

JDK1.8 中会,Java 发放了 CompletableFuture 类,它是基于异步函数双管演算。相对堵塞双管继续前进返回结果,CompletableFuture 可以通过回调的方双管也来管控计算结果,构建了异步非堵塞,精度更是优。

CompletableFuture 构建了 Future 和 CompletionStage 终端, 都将放了多种构建异步演算的原理,如supplyAsync, runAsync以及thenApplyAsync。

示例我们适用CompletableFuture来构建上面的范例:

CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));while (!completableFuture.isDone()) { System.out.println("CompletableFuture is not finished yet...");}long result = completableFuture.get();

我们不必需显双管适用 ExecutorService,CompletableFuture 内部适用了 ForkJoinPool 来管控异步目标,这使得我们的代码变的更是典雅。

四、springBoot @Async异步

在@Async节录之前,适用多虚拟机必需适用JDK的原生原理,十分麻烦,人口为120人了@Async便就比较十分简单了。

首先,适用 @EnableAsync 启用异步节录:

@SpringBootApplication@EnableAsyncpublic class StartApplication { public static void main(String[] args) { SpringApplication.run(StartApplication.class, args); }}

自定义虚拟机池水:

@Configuration@Slf4jpublic class ThreadPoolConfiguration { @Bean(name = "defaultThreadPoolExecutor", destroyMethod = "shutdown") public ThreadPoolExecutor systemCheckPoolExecutorService() { return new ThreadPoolExecutor(3, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(10000), new ThreadFactoryBuilder().setNameFormat("default-executor-%d").build(), (r, executor) -> log.error("system pool is full! ")); }}

在异步管控的原理上附加节录 @Async ,当对 execute 原理 codice_时,通过自定义的虚拟机池水 defaultThreadPoolExecutor 异步化督导 execute 原理

@Servicepublic class AsyncServiceImpl implements AsyncService { @Async("defaultThreadPoolExecutor") public Boolean execute(Integer num) { System.out.println("虚拟机:" + Thread.currentThread().getName() + " , 目标:" + num); return true; }}

用 @Async 节录标记的原理,称为异步原理。在spring boot应用中会适用 @Async 很十分简单:

codice_异步原理类上或者启动类突显节录 @EnableAsync在必需被异步codice_的原理以外突显 @Async所适用的 @Async 节录原理的类单纯应该是Spring器皿管理的bean单纯;五、Guava异步

Guava 发放了 ListenableFuture 类来督导异步系由统设计

1.首先我们必需附加 guava 的maven依赖:

com.google.guava guava 28.2-jre

2.现在我们适用ListenableFuture来构建我们之前的范例:

ExecutorService threadpool = Executors.newCachedThreadPool();ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);ListenableFuture guavaFuture = (ListenableFuture) service.submit(()-> factorial(number));long result = guavaFuture.get();

这里适用MoreExecutors获取ListeningExecutorService类的模板,然后ListeningExecutorService.submit督导异步目标,并返回 ListenableFuture模板。

Java异步演算小结

异步演算受到了越来越多的注目,偏爱是在 IO 密集型的金融业务场景中会,相比传统的启动时开发设计模双管,异步演算的优势越来越明显,希望以上介绍的5种Java异步演算方双管也对你有所希望!

以上!

精选集●架构技术干货

1.分布双管架构设计从0到1全部精选集,遇到困难珍品!

2.Java多线与都将演算从0到1全部精选集,遇到困难珍品!

3.JVM虚拟机系由从0到1全部精选集,遇到困难珍品!

4.Spring系由列从0到1全部精选集,遇到困难珍品!

5.Java设计模双管:23种设计模双管(万字图文新一轮说明了)

6.史上屈指可数劲消息链表MQ万字图文说明了,遇到困难珍品!

秋冬经常便秘是什么原因
复方鱼腥草合剂
调理脾胃治疗消化不良的中药有哪些马上告诉你
胃反酸吃金奥康奥美拉唑怎么样
总是便秘还容易拉肚子怎么办
标签:方式
友情链接: