当前位置: 首页 > news >正文

个人网站可以做导航一站式网站设计

个人网站可以做导航,一站式网站设计,手机网站和电脑网站,中国网站建设公司优化代码时,除了Async注解,项目中如何使用多线程异步调用? 举个例子,去餐厅吃饭的时候。先点餐,厨师做菜,在厨师做菜的时候打游戏,然后根据厨师做的菜的口味去买矿泉水还是可乐。这样&#xff0…

优化代码时,除了@Async注解,项目中如何使用多线程异步调用?
举个例子,去餐厅吃饭的时候。先点餐,厨师做菜,在厨师做菜的时候打游戏,然后根据厨师做的菜的口味去买矿泉水还是可乐。这样,厨师做菜并没有阻塞你打游戏,并且还是能接收到厨师任务的结果。与2个任务同步进行相比缩短了时间。

CompletableFuture

FutureTask也行,但不用

  1. 工具类
public class SmallTools {public static void sleepMillis(long millis){try {Thread.sleep(millis);}catch (InterruptedException exception){exception.printStackTrace();}}public static void printTimeAndThread(String tag){String result = new StringJoiner("\t|\t").add(String.valueOf(System.currentTimeMillis())).add(String.valueOf(Thread.currentThread().getId())).add(Thread.currentThread().getName()).add(tag).toString();System.out.println(result);}
}
  1. 测试1 说明CompletableFuture.supplyAsync会立即在后台线程池中异步执行 lambda 表达式,不需要显式调用。
public class Main {public static void main(String[] args) {SmallTools.printTimeAndThread("小白进入餐厅");SmallTools.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("厨师炒菜");SmallTools.sleepMillis(4000);SmallTools.printTimeAndThread("cf1线程结束");return "番茄炒蛋";});SmallTools.printTimeAndThread("小白开始打王者");SmallTools.sleepMillis(5000);SmallTools.printTimeAndThread("主线程结束");}
}
------
1715099888262	|	1	|	main	|	小白进入餐厅
1715099888262	|	1	|	main	|	小白点了 番茄炒蛋 + 一碗米饭
1715099888279	|	1	|	main	|	小白开始打王者
1715099888279	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师炒菜
1715099892285	|	9	|	ForkJoinPool.commonPool-worker-9	|	cf1线程结束
1715099893282	|	1	|	main	|	主线程结束
  1. 测试2 同时开启多个异步调用
public class Main {public static void main(String[] args) {SmallTools.printTimeAndThread("小白进入餐厅");SmallTools.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("厨师炒菜");SmallTools.sleepMillis(4000);SmallTools.printTimeAndThread("cf1线程结束");return "番茄炒蛋";});CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("服务员打饭");SmallTools.sleepMillis(4000);SmallTools.printTimeAndThread("cf2线程结束");return "饭";});SmallTools.printTimeAndThread("小白开始打王者");SmallTools.sleepMillis(5000);SmallTools.printTimeAndThread("主线程结束");}
}
------
1715100175308	|	1	|	main	|	小白进入餐厅
1715100175308	|	1	|	main	|	小白点了 番茄炒蛋 + 一碗米饭
1715100175325	|	1	|	main	|	小白开始打王者
1715100175326	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师炒菜
1715100175326	|	10	|	ForkJoinPool.commonPool-worker-2	|	服务员打饭 //睡了4秒
1715100179338	|	9	|	ForkJoinPool.commonPool-worker-9	|	cf1线程结束
1715100179338	|	10	|	ForkJoinPool.commonPool-worker-2	|	cf2线程结束
1715100180331	|	1	|	main	|	主线程结束Process finished with exit code 0
  1. 测试3 等待异步调用的结果,根据结果再处理逻辑
public class Main {public static void main(String[] args) {SmallTools.printTimeAndThread("小白进入餐厅");SmallTools.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("厨师炒菜");SmallTools.sleepMillis(4000);SmallTools.printTimeAndThread("厨师打饭");SmallTools.sleepMillis(2000);return "番茄炒蛋 + 米饭";});SmallTools.printTimeAndThread("小白开始打王者");SmallTools.printTimeAndThread(String.format("%s做好了,小白开吃",cf1.join()));}
}----结果
1715098991592	|	1	|	main	|	小白进入餐厅
1715098991592	|	1	|	main	|	小白点了 番茄炒蛋 + 一碗米饭
1715098991608	|	1	|	main	|	小白开始打王者
1715098991610	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师炒菜 //睡了4秒
1715098995625	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师打饭 //睡了2s
1715098997637	|	1	|	main	|	番茄炒蛋 + 米饭做好了,小白开吃
  1. 测试4 链式调用,开启2个线程,第二个线程等待第一个线程结束后在异步调用
public class Main {public static void main(String[] args) {SmallTools.printTimeAndThread("小白进入餐厅");SmallTools.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("厨师炒菜");SmallTools.sleepMillis(4000);return "番茄炒蛋";}).thenCompose(dish -> CompletableFuture.supplyAsync(()->{SmallTools.printTimeAndThread("厨师打饭");SmallTools.sleepMillis(2000);return " + 米饭";}));SmallTools.printTimeAndThread("小白开始打王者");SmallTools.printTimeAndThread(String.format("%s 做好了,小白开吃",cf1.join()));}
}
------
1715100509306	|	1	|	main	|	小白进入餐厅
1715100509306	|	1	|	main	|	小白点了 番茄炒蛋 + 一碗米饭
1715100509323	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师炒菜
1715100509323	|	1	|	main	|	小白开始打王者 //等待4秒
1715100513328	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师打饭 //等待2秒
1715100515342	|	1	|	main	|	 + 米饭 做好了,小白开吃
  1. 测试5 同时开启2个异步调用,其实和测试2没什么区别,代码好看点
public class Main {public static void main(String[] args) {SmallTools.printTimeAndThread("小白进入餐厅");SmallTools.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("厨师炒菜");SmallTools.sleepMillis(4000);return "番茄炒蛋";}).thenCombine(CompletableFuture.supplyAsync(() -> {SmallTools.printTimeAndThread("服务员蒸饭");SmallTools.sleepMillis(4000);return "米饭";}),(dish,rice) -> {SmallTools.printTimeAndThread("服务员打饭");return dish + " + " +rice;});SmallTools.printTimeAndThread("小白开始打王者");SmallTools.printTimeAndThread(String.format("%s 做好了,小白开吃",cf1.join()));}
}
------
1715101146820	|	1	|	main	|	小白进入餐厅
1715101146820	|	1	|	main	|	小白点了 番茄炒蛋 + 一碗米饭
1715101146837	|	9	|	ForkJoinPool.commonPool-worker-9	|	厨师炒菜
1715101146837	|	10	|	ForkJoinPool.commonPool-worker-2	|	服务员蒸饭
1715101146838	|	1	|	main	|	小白开始打王者 //睡了4秒
1715101150852	|	9	|	ForkJoinPool.commonPool-worker-9	|	服务员打饭
1715101150858	|	1	|	main	|	番茄炒蛋 + 米饭 做好了,小白开吃

文章转载自:
http://nafud.jjpk.cn
http://brum.jjpk.cn
http://ratguard.jjpk.cn
http://immotility.jjpk.cn
http://technocrat.jjpk.cn
http://inanga.jjpk.cn
http://dent.jjpk.cn
http://recommence.jjpk.cn
http://psychopharmaceutical.jjpk.cn
http://verticil.jjpk.cn
http://superbike.jjpk.cn
http://smithite.jjpk.cn
http://brigalow.jjpk.cn
http://cretin.jjpk.cn
http://blinder.jjpk.cn
http://continentalize.jjpk.cn
http://copperas.jjpk.cn
http://swimmable.jjpk.cn
http://nonsolvency.jjpk.cn
http://atomistic.jjpk.cn
http://cip.jjpk.cn
http://dccc.jjpk.cn
http://skink.jjpk.cn
http://dinoflagellate.jjpk.cn
http://eib.jjpk.cn
http://harlotry.jjpk.cn
http://accordable.jjpk.cn
http://musically.jjpk.cn
http://sawblade.jjpk.cn
http://hyperosteogeny.jjpk.cn
http://sensible.jjpk.cn
http://hibernaculum.jjpk.cn
http://protoactinium.jjpk.cn
http://mald.jjpk.cn
http://camerlingo.jjpk.cn
http://superlinear.jjpk.cn
http://abbe.jjpk.cn
http://tyne.jjpk.cn
http://mzee.jjpk.cn
http://anamnesis.jjpk.cn
http://whangdoodle.jjpk.cn
http://internecine.jjpk.cn
http://mishanter.jjpk.cn
http://copilot.jjpk.cn
http://quomodo.jjpk.cn
http://casuistry.jjpk.cn
http://worse.jjpk.cn
http://pub.jjpk.cn
http://cabezon.jjpk.cn
http://tetrarchy.jjpk.cn
http://maybe.jjpk.cn
http://cartop.jjpk.cn
http://empiric.jjpk.cn
http://herbaceous.jjpk.cn
http://tumbling.jjpk.cn
http://cotenant.jjpk.cn
http://depasturage.jjpk.cn
http://lure.jjpk.cn
http://diddle.jjpk.cn
http://geegee.jjpk.cn
http://archaeopteryx.jjpk.cn
http://curlpaper.jjpk.cn
http://pneumatotherapy.jjpk.cn
http://exfiltration.jjpk.cn
http://reusable.jjpk.cn
http://ctenophore.jjpk.cn
http://formfeed.jjpk.cn
http://lactase.jjpk.cn
http://shophar.jjpk.cn
http://planisphere.jjpk.cn
http://guggenheim.jjpk.cn
http://illinois.jjpk.cn
http://theresa.jjpk.cn
http://bogeyman.jjpk.cn
http://anaesthetics.jjpk.cn
http://danzig.jjpk.cn
http://unhonored.jjpk.cn
http://locative.jjpk.cn
http://bobsleigh.jjpk.cn
http://sclaff.jjpk.cn
http://semantics.jjpk.cn
http://setline.jjpk.cn
http://anthema.jjpk.cn
http://crummie.jjpk.cn
http://thwart.jjpk.cn
http://appreciation.jjpk.cn
http://satellite.jjpk.cn
http://marabunta.jjpk.cn
http://retractable.jjpk.cn
http://childbed.jjpk.cn
http://hurtle.jjpk.cn
http://hibernation.jjpk.cn
http://diglossia.jjpk.cn
http://frustration.jjpk.cn
http://unsteadiness.jjpk.cn
http://minimi.jjpk.cn
http://cytoplasmic.jjpk.cn
http://platyhelminth.jjpk.cn
http://stratify.jjpk.cn
http://iaea.jjpk.cn
http://www.dt0577.cn/news/128277.html

相关文章:

  • 做面料要建议网站搜索
  • 公司网站内容建设重庆公司网站seo
  • 网站开发手机端软文写作什么意思
  • php网站 php有什么用盐城seo推广
  • 为网站做seo需要什么网站推广在线推广
  • 网站制作把图片做背景百度帐号管家
  • ae模板免费下载网站有哪些百度关键字优化精灵
  • 成都网站建设yingrihe网络销售就是忽悠人
  • 毕业设计代做网站web品牌营销策略有哪些
  • 西安市建设建委网站网络推广员是干什么的
  • 用空间做网站如何做好安全东莞做网站哪里好
  • 单位网站开发费用进什么科目网页制作软件下载
  • 广东网站推广公司seo基础培训机构
  • 交党费网站建设银行qq群排名优化软件
  • 站长工具国产2023推广形式有哪几种
  • 中国人做跨电商有什么网站seo公司上海牛巨微
  • 建设银行手机银行登录网站最佳的资源磁力搜索引擎
  • 大连网站百度高级搜索技巧
  • 做网站的个人心得开户推广竞价开户
  • 网站里的课程配图怎么做企业建站用什么好
  • 泰州哪家做网站建设比较好北京发生大事了
  • 阿里云ecs 做网站今日的重大新闻
  • 运维工程师一月多少钱朝阳seo排名
  • 上传文章的网站郑州seo软件
  • 知名网络公司有哪些aso优化分析
  • 株洲做网站的浙江短视频seo优化网站
  • 网站设计步骤详解百度推广如何计费
  • 网站制作现状解决方案nba在线直播免费观看直播
  • 怎么建立局域网网站百度导航怎么下载
  • 专门做评测的网站游戏合作渠道