当前位置:网站首页>Completable future practical usage
Completable future practical usage
2022-07-26 13:43:00 【CS beat you】
import com.atguigu.common.utils.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ThreadPoolExecutor; @RestController @RequestMapping("/product/thread") public class ThreadTestController { @Autowired private ThreadPoolExecutor threadPoolExecutor; @RequestMapping("/test") public R test() throws Exception { long start = System.currentTimeMillis(); // First step , Asynchronous execution : no return value CompletableFuture<String> main = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(" Performed the first task ...."); return "main"; }, threadPoolExecutor); // The second step , Wait for the completion of the first step to execute asynchronously CompletableFuture<Void> two = main.thenAcceptAsync((res) -> { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(" The second task was performed ....\t" + res); }, threadPoolExecutor); // The third step , Wait for the completion of the first step to execute asynchronously CompletableFuture<Void> thread = main.thenAcceptAsync((res) -> { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(" Performed the third task ....\t" + res); }, threadPoolExecutor); // Wait for all to complete CompletableFuture.allOf(main,two,thread).get(); System.out.println(" Main thread execution completed ....."); long end = System.currentTimeMillis(); System.out.println("main Always use time :"+(end-start)); // So it took a total of six seconds return R.ok(); } }
边栏推荐
- MySQL data directory (1) -- database structure (24)
- I. creation and constraint of MySQL table
- 带你熟悉云网络的“电话簿”:DNS
- Basic sentence structure of English ----- origin
- Algorithm -- continuous sequence (kotlin)
- Click El dropdown item/@click.native
- Win11+VS2019配置YOLOX
- JUC summary
- Pytorch学习笔记(一)安装与常用函数的使用
- B+ tree index use (9) grouping, back to table, overlay index (21)
猜你喜欢

ROS2学习(1)ROS2简述

Probability theory and mathematical statistics

多线程使用不当导致的 OOM

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

《Kotlin系列》之MVVM架构封装(kotlin+mvvm)

最新战报:十项认证,五项最佳实践

Solve the problem that the remote host cannot connect to the MySQL database

The last time I heard about eBay, or the last time

Feixin, which lasted 15 years and had 500million users, was completely dead

Uncover the secret of white hat: 100 billion black products on the Internet scare musk away
随机推荐
How to remove black edges from hyperimage images (two methods)
云智技术论坛工业专场 明天见!
421. Maximum XOR value of two numbers in the array
Convert the array in JSON file to struct
Detailed explanation of factory mode
AI theory knowledge map 1 Foundation
Force deduction ----- the number of words in the string
The use of asynchronous thread pool in development
Intercept the coordinate points (four point coordinates of the face frame) face image from the marked XML file and save it in the specified folder
图扑 3D 可视化国风设计 | 科技与文化碰撞炫酷”火花“
flutter多渠道打包运行
Codeforces round 810 (Div. 2) [competition record]
B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
POM文件详解
How to write the introduction of GIS method journals and papers?
带你熟悉云网络的“电话簿”:DNS
SuperMap iclient for leaflet loads Gauss Kruger projection three-dimensional zonation CGCS2000 geodetic coordinate system WMTs service
Algorithm -- continuous sequence (kotlin)
Official announcement! Edweisen group and Baidu xirang reached a deep co creation cooperation
B+ tree (4) joint index -- MySQL from entry to proficiency (16)