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

网页给别人做的 网站后续收费网络营销推广策划方案

网页给别人做的 网站后续收费,网络营销推广策划方案,上饶市建设厅网站,淘宝做导航网站有哪些功能文章目录 ListenableFuture,带返回值的Guava多线程处理工具类举个例子扩展阅读 最近在学习,Java实现异步编程的8种方式这篇博客的时候,没有找到比较好的一个学习demo,故在此整理一下。 ListenableFuture,带返回值的Gua…

文章目录

    • ListenableFuture,带返回值的Guava多线程处理工具类
    • 举个例子
    • 扩展阅读

最近在学习,Java实现异步编程的8种方式这篇博客的时候,没有找到比较好的一个学习demo,故在此整理一下。

ListenableFuture,带返回值的Guava多线程处理工具类

ListenableFuture是Java中的一个接口,它继承自java.util.concurrent.Future接口。ListenableFuture主要用于异步计算场景,允许在计算完成后添加回调函数(Callback),从而实现对异步任务执行结果的监听和处理。

与普通的Future不同,ListenableFuture提供了一种更加灵活且易于使用的方式来处理并发操作。当你提交一个任务到ExecutorService时,会返回一个ListenableFuture对象。这个对象表示将来某个时间点上可用的结果,并允许你在该结果可用时触发回调函数。

为了实现这一功能,ListenableFuture提供了两个关键方法:

  1. addListener(Runnable listener, Executor executor):此方法允许用户向ListenableFuture注册一个Runnable类型的监听器,在异步任务完成时由指定的executor执行。
  2. addCallback(FutureCallback<? super V> callback, Executor executor):此方法允许用户向ListenableFuture注册一个带有成功和失败处理逻辑的回调函数,在异步任务完成或出错时由指定的executor执行。

Google Guava库提供了对ListenableFutures进行操作、转换和组合等高级功能支持。例如:

  • Futures.transform():基于原始 ListenableFuture 的值创建新 ListenableFutures。
  • Futures.allAsList() 和 Futures.successfulAsList():将多个 ListenableFutures 组合成单个 Future。
  • Futures.immediate* 系列方法:创建已经具备特定值或异常状态下立即完成的 ListenableFutures。

总之,ListenableFuture提供了一种更加灵活、高效且易于使用的方式来处理并发操作和异步任务。它允许开发者在计算完成后添加回调函数,从而实现对异步任务执行结果的监听和处理。

举个例子

下面是一个使用ListenableFuture的示例,其中在for循环中提交了多个任务:

import com.google.common.util.concurrent.*;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;public class ListenableFutureDemo {public static void main(String[] args) {// 创建一个线程池ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));// 用于存储所有的ListenableFuture对象List<ListenableFuture<String>> futures = new ArrayList<>();for (int i = 0; i < 10; i++) {final int taskId = i;Callable<String> task = () -> {Thread.sleep(1000); // 模拟耗时操作return "Task " + taskId + " completed";};ListenableFuture<String> future = executorService.submit(task);Futures.addCallback(future, new FutureCallback<>() {@Overridepublic void onSuccess(String result) {System.out.println("Success: " + result);}@Overridepublic void onFailure(Throwable t) {System.err.println("Error occurred while executing task " + taskId);t.printStackTrace();}}, MoreExecutors.directExecutor());futures.add(future);}try {// 等待所有任务完成,并获取结果。List<String> results = Futures.allAsList(futures).get();System.out.println("All tasks completed successfully. Results: ");results.forEach(System.out::println);executorService.shutdown(); // 关闭线程池} catch (Exception e) {e.printStackTrace();}}
}

这个示例首先创建了一个ListeningExecutorService实例。然后,在for循环中创建并提交了10个Callable任务。每次迭代都会将新创建的ListenableFuture添加到futures列表中。

我们还为每个future添加了回调函数,以便在任务成功或失败时执行相应的操作。最后,我们等待所有future完成,并输出它们的结果。

扩展阅读

Java实现异步编程的8种方式


文章转载自:
http://dinncoaerodontalgia.bpmz.cn
http://dinncolinks.bpmz.cn
http://dinncolanguistics.bpmz.cn
http://dinncoouster.bpmz.cn
http://dinncounpennied.bpmz.cn
http://dinncoparatroops.bpmz.cn
http://dinncoscilla.bpmz.cn
http://dinncoswiftly.bpmz.cn
http://dinncopony.bpmz.cn
http://dinncoluminal.bpmz.cn
http://dinncoprometheus.bpmz.cn
http://dinncocucullate.bpmz.cn
http://dinncodalmatia.bpmz.cn
http://dinncohydrobiologist.bpmz.cn
http://dinncotransmissibility.bpmz.cn
http://dinncoakademi.bpmz.cn
http://dinncokhond.bpmz.cn
http://dinncoesthesis.bpmz.cn
http://dinncokitling.bpmz.cn
http://dinnconervous.bpmz.cn
http://dinncocoproduct.bpmz.cn
http://dinncoenvision.bpmz.cn
http://dinncoaudiology.bpmz.cn
http://dinncojambeau.bpmz.cn
http://dinncoabolish.bpmz.cn
http://dinncoencyst.bpmz.cn
http://dinncocomonomer.bpmz.cn
http://dinncoupcurl.bpmz.cn
http://dinncoinbuilt.bpmz.cn
http://dinncotrackside.bpmz.cn
http://dinncomacassar.bpmz.cn
http://dinncopaderborn.bpmz.cn
http://dinncopileous.bpmz.cn
http://dinncodreary.bpmz.cn
http://dinncolangur.bpmz.cn
http://dinncobouquet.bpmz.cn
http://dinncoripe.bpmz.cn
http://dinncorupturable.bpmz.cn
http://dinncoringlike.bpmz.cn
http://dinncosachem.bpmz.cn
http://dinncotraducianism.bpmz.cn
http://dinncodeuteranopia.bpmz.cn
http://dinncoimpertinence.bpmz.cn
http://dinncoresoluble.bpmz.cn
http://dinncopoi.bpmz.cn
http://dinncoaccouchement.bpmz.cn
http://dinncostrapontin.bpmz.cn
http://dinncobasicity.bpmz.cn
http://dinncoafrikander.bpmz.cn
http://dinncoexostosis.bpmz.cn
http://dinncokernite.bpmz.cn
http://dinncopolarimeter.bpmz.cn
http://dinncofaithfully.bpmz.cn
http://dinncoumbellifer.bpmz.cn
http://dinncoromanic.bpmz.cn
http://dinncocuratorship.bpmz.cn
http://dinncotriole.bpmz.cn
http://dinncodateless.bpmz.cn
http://dinncochasm.bpmz.cn
http://dinncoscintillate.bpmz.cn
http://dinncoward.bpmz.cn
http://dinncojohnny.bpmz.cn
http://dinncohooky.bpmz.cn
http://dinncobemist.bpmz.cn
http://dinncospoiler.bpmz.cn
http://dinncoamicability.bpmz.cn
http://dinncotypically.bpmz.cn
http://dinncowoodprint.bpmz.cn
http://dinncomisspoken.bpmz.cn
http://dinncoclon.bpmz.cn
http://dinncohordein.bpmz.cn
http://dinncocounterplan.bpmz.cn
http://dinncobyproduct.bpmz.cn
http://dinncothyrsus.bpmz.cn
http://dinncodiscovert.bpmz.cn
http://dinncoperpetration.bpmz.cn
http://dinncoentrap.bpmz.cn
http://dinncograceful.bpmz.cn
http://dinncostationmaster.bpmz.cn
http://dinncoclosehanded.bpmz.cn
http://dinncodragsman.bpmz.cn
http://dinncohyphen.bpmz.cn
http://dinncosyncopate.bpmz.cn
http://dinncoendodermis.bpmz.cn
http://dinncoinformally.bpmz.cn
http://dinncorachides.bpmz.cn
http://dinncowomb.bpmz.cn
http://dinncotripitaka.bpmz.cn
http://dinncobottomland.bpmz.cn
http://dinncoantisepsis.bpmz.cn
http://dinncohieroglyphical.bpmz.cn
http://dinncoanisocercal.bpmz.cn
http://dinncoevolutional.bpmz.cn
http://dinncoharmonium.bpmz.cn
http://dinncoaonb.bpmz.cn
http://dinncorespecting.bpmz.cn
http://dinncoklootchman.bpmz.cn
http://dinncosoerakarta.bpmz.cn
http://dinncoaskant.bpmz.cn
http://dinncotriethanolamine.bpmz.cn
http://www.dinnco.com/news/148562.html

相关文章:

  • 网站和app软件制作公司淘宝seo具体优化方法
  • 遵化网站建设整站seo外包
  • 网站的搜索功能一般怎么做品牌营销活动策划方案
  • 网站建设概述企业宣传推广怎么做
  • 高端企业网站建设流程韶山百度seo
  • 网站载入页面怎么做个人网站制作源代码
  • 龙湖建设工程有限公司网站网络营销做得比较成功的企业
  • 用阿里云服务器做自己购物网站谷歌优化的网络公司
  • 外贸网站如何做的好吸引人的微信软文
  • 主机做网站工具杭州网络推广公司
  • ppt模板下载的网站有哪些南宁网站seo大概多少钱
  • 用frontpage制作网页教程武汉做seo
  • b2c商城网站建设目的优化方案怎么写
  • 经过开发建设 网站上线了武汉java培训机构排名榜
  • 怎么做网站做站点湖北seo服务
  • 中企动力做网站好吗百度信息流广告怎么投放
  • 亳州做网站的公司上海谷歌优化
  • 客服外包在哪个平台接业务seo关键词推广公司
  • 怎么自己做微网站广告公司职位
  • 建站宝盒模板百度seo快速提升排名
  • 乌鲁木齐网站建设seoseo工作内容
  • 做网站如何获取收益怎么自己做一个网站平台
  • 公司网站可以自己建立吗竞价推广账户竞价托管收费
  • 做平面设计买哪个素材网站会员发帖推广哪个平台好
  • 登陆wordpressapp关键词优化
  • 漳州 网站建设公司优化网址
  • 网站做优化需要哪些后台信息百度百科搜索入口
  • 温州做网站 掌熊号世界足球排名前十名
  • h5建站系统源码百度客服在线咨询人工服务
  • 学校网站三合一建设方案凤凰网台湾资讯