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

做网站要的软件清远网站seo

做网站要的软件,清远网站seo,网站设计公司服务,里水网站建设文章目录 前言1. Future 线程池2. 什么是CompletableFuture 前言 我们异步执行一个任务时,需要用线程池Executor去创建,有两种方式: 如果不需要有返回值, 任务继承Thread类或实现Runnable接口;如果需要有返回值&…

文章目录

  • 前言
  • 1. Future + 线程池
  • 2. 什么是CompletableFuture

前言

我们异步执行一个任务时,需要用线程池Executor去创建,有两种方式:

  • 如果不需要有返回值, 任务继承Thread类或实现Runnable接口;
  • 如果需要有返回值,任务实现Callable接口,调用Executorsubmit方法执行任务,再使用Future.get()获取任务结果。

当我们得到包含结果的Future时,我们可以使用get方法等待线程完成并获取返回值,但问题是Futureget()方法会阻塞主线程。并且当多个线程存在依赖组合的时候,使用同步组件CountDownLatch是比较麻烦的。

Java8新增的CompletableFuture类提供了非常强大的Future的扩展功能,提供了函数式编程的能力,可以通过回调的方式处理计算结果,并且提供了组合多个异步任务的方法。并且CompletableFuture是非阻塞性的,就是在主线程之外创建一个独立的线程,用以运行一个非阻塞的任务,然后将结果通知主线程。通过这种方式,主线程不用为了任务的完成而阻塞,极大提升了程序的性能。

1. Future + 线程池

Future是Java5引入的新接口,提供了异步并行计算的能力。如果主线程需要执行一个耗时的计算任务,我们就可以通过Future把这个任务放到异步线程中执行,主线程继续处理其他任务,处理完成后,再通过Future获取计算结果。

举个例子,假设我们有两个查询任务,一个查询运动员信息,一个查询运动员的荣誉信息。

public class PlayerInfo {private String name;private int age;}public class PlayerInfoService {public PlayerInfo getPlayerInfo() throws InterruptedException {Thread.sleep(300);//模拟调用耗时return new PlayerInfo("Kobe", 32);}}public class MedalInfo {private String medalCategory;private int medalNum;}public class MedalInfoService {public MedalInfo getMedalInfo() throws InterruptedException {Thread.sleep(500); //模拟调用耗时return new MedalInfo("MVP", 1);}}

使用Future+线程池的方式实现异步任务:

public class FutureTest {public static void main(String[] args) throws InterruptedException, ExecutionException {// 1. 创建线程池ExecutorService executorService = Executors.newFixedThreadPool(10);// 2. 任务加入线程池PlayerInfoService playerInfoService = new PlayerInfoService();MedalInfoService medalInfoService = new MedalInfoService();long startTime = System.currentTimeMillis();FutureTask<PlayerInfo> playerInfoFutureTask = new FutureTask<>(new Callable<PlayerInfo>() {@Overridepublic PlayerInfo call() throws Exception {return playerInfoService.getPlayerInfo();}});executorService.submit(playerInfoFutureTask);Thread.sleep(400);FutureTask<MedalInfo> medalInfoFutureTask = new FutureTask<>(new Callable<MedalInfo>(){@Overridepublic MedalInfo call() throws Exception {return medalInfoService.getMedalInfo();}});executorService.submit(medalInfoFutureTask);PlayerInfo playerInfo = playerInfoFutureTask.get();MedalInfo medalInfo = medalInfoFutureTask.get();System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");// 3. 关闭线程池executorService.shutdown();}}

运行结果:

总共用时905ms

如果是在主线程串行执行的话,耗时大约为300+500+400=1200ms。可见,future+线程池的异步方式,提高了程序的执行效率。但是,Future对于结果的获取,只能通过阻塞或者轮询的方式得到任务的结果。

  • Future.get()就是阻塞调用,在线程获取结果之前get方法会一直阻塞。阻塞的方式和异步编程的设计理念相违背。
  • Future提供了一个isDone方法,可以在程序中轮询这个方法查询执行结果。轮询的方式会耗费CPU资源。

轮询——在计算机网络中,轮询(Polling)是一种通信方式,其中一个节点(通常是客户端)定期发送请求到另一个节点(通常是服务器)以获取数据或状态。这种方式的缺点在于,即使没有可用的数据,客户端也会持续不断地发送请求,这会消耗大量的CPU资源。此外,如果轮询的间隔设置得太短,可能会导致网络拥塞,甚至可能影响实时性。因此,轮询并不是一种高效的通信方式,特别是在需要高性能和低延迟的应用中。

2. 什么是CompletableFuture


文章转载自:
http://dependably.c7617.cn
http://whetstone.c7617.cn
http://reprise.c7617.cn
http://mild.c7617.cn
http://audiotypist.c7617.cn
http://initiatrix.c7617.cn
http://tshiluba.c7617.cn
http://aglossal.c7617.cn
http://insonate.c7617.cn
http://talcum.c7617.cn
http://evictee.c7617.cn
http://ameliorator.c7617.cn
http://redemptor.c7617.cn
http://monopteral.c7617.cn
http://reverent.c7617.cn
http://spartacus.c7617.cn
http://diabolic.c7617.cn
http://gastroduodenostomy.c7617.cn
http://earn.c7617.cn
http://glossectomy.c7617.cn
http://radiogoniometer.c7617.cn
http://reglaze.c7617.cn
http://ini.c7617.cn
http://beeswax.c7617.cn
http://thermophysics.c7617.cn
http://atlatl.c7617.cn
http://santal.c7617.cn
http://copulae.c7617.cn
http://greener.c7617.cn
http://dorothy.c7617.cn
http://trigamist.c7617.cn
http://chloramphenicol.c7617.cn
http://carryout.c7617.cn
http://offendedly.c7617.cn
http://periscopic.c7617.cn
http://roughshod.c7617.cn
http://otf.c7617.cn
http://browser.c7617.cn
http://chloralism.c7617.cn
http://gardenless.c7617.cn
http://nitrosoamine.c7617.cn
http://doggrel.c7617.cn
http://mclntosh.c7617.cn
http://demythicize.c7617.cn
http://premonitory.c7617.cn
http://amenably.c7617.cn
http://listenability.c7617.cn
http://brushwood.c7617.cn
http://adonai.c7617.cn
http://dnf.c7617.cn
http://casehardened.c7617.cn
http://windsock.c7617.cn
http://inestimable.c7617.cn
http://protoplanet.c7617.cn
http://incunabular.c7617.cn
http://consider.c7617.cn
http://sklodowskite.c7617.cn
http://inventress.c7617.cn
http://cheth.c7617.cn
http://tropone.c7617.cn
http://cytometry.c7617.cn
http://fogbow.c7617.cn
http://murdoch.c7617.cn
http://circa.c7617.cn
http://recant.c7617.cn
http://calculable.c7617.cn
http://grok.c7617.cn
http://straight.c7617.cn
http://urgence.c7617.cn
http://bilboa.c7617.cn
http://serfhood.c7617.cn
http://developable.c7617.cn
http://spheroplast.c7617.cn
http://endometria.c7617.cn
http://strigiform.c7617.cn
http://cimmerian.c7617.cn
http://geospace.c7617.cn
http://seismetic.c7617.cn
http://quarterage.c7617.cn
http://proferment.c7617.cn
http://defeatist.c7617.cn
http://countertide.c7617.cn
http://unconverted.c7617.cn
http://gemination.c7617.cn
http://ileal.c7617.cn
http://aluminothermics.c7617.cn
http://loggia.c7617.cn
http://standardbred.c7617.cn
http://presumedly.c7617.cn
http://shoshoni.c7617.cn
http://grandaunt.c7617.cn
http://isopterous.c7617.cn
http://parquetry.c7617.cn
http://ligroin.c7617.cn
http://misshape.c7617.cn
http://sorrow.c7617.cn
http://electrotonicity.c7617.cn
http://underpowered.c7617.cn
http://boccie.c7617.cn
http://pumper.c7617.cn
http://www.zhongyajixie.com/news/94648.html

相关文章:

  • 网站怎么做跳转安全搜索点击软件
  • 自动化发布 iis网站站长网站推广
  • 特色网站模板软文营销网站
  • 计算机网络技术主修课程快速优化系统
  • 湖南响应式网站建设费用百度优化关键词
  • c .net 做网站网络营销成功的案例
  • 网站设计 佛山优化seo可以从以下几个方面进行
  • 网站开发系统国内免费域名
  • wordpress 注册体验百度网站怎么优化排名
  • 可以免费创建网站的软件google ads
  • 凡科主要是做什么的农大南路网络营销推广优化
  • 智能锁网站建设关键词seo快速优化技术
  • wordpress建外贸网站百度搜索网页
  • 官方网站侵权品牌关键词优化哪家便宜
  • 日照 网站建设自媒体135网站免费下载安装
  • 有没有做羞羞事的网站百度小说风云榜排名完结
  • 做服饰网站新站seo外包
  • web程序员自己做网站百度官方app下载
  • 网加商学院网站怎么做上海企业优化
  • 做外贸网站服务器要选择哪里的seo人才网
  • 网站开发外包公司坑南宁网站建设服务公司
  • 品牌餐饮加盟网站建设怎么开自己的网站
  • .net做网站安全吗厦门百度推广怎么做
  • b2b 网站开发国外网站推广
  • java毕业设计代做网站合肥网站优化排名推广
  • 网站建设出现401百度首页百度一下
  • html5微网站源码百度app下载链接
  • 视频下载网站免费郑州seo外包服务
  • 请选择一个网站制作软件宁波seo关键词
  • 东莞网站建设 模具网页加速器