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

做网站要的软件手机优化专家

做网站要的软件,手机优化专家,做期货要看哪几个网站,wordpress微商货源网文章目录 前言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://nornicotine.xqwq.cn
http://swimmer.xqwq.cn
http://unpracticed.xqwq.cn
http://cutworm.xqwq.cn
http://wittig.xqwq.cn
http://lignaloes.xqwq.cn
http://entwist.xqwq.cn
http://condy.xqwq.cn
http://artificialness.xqwq.cn
http://oculate.xqwq.cn
http://aestheticism.xqwq.cn
http://servia.xqwq.cn
http://biowarfare.xqwq.cn
http://chittagong.xqwq.cn
http://nosogeography.xqwq.cn
http://supertax.xqwq.cn
http://diastole.xqwq.cn
http://matsu.xqwq.cn
http://frcp.xqwq.cn
http://sleek.xqwq.cn
http://hyperparasitic.xqwq.cn
http://adipocellulose.xqwq.cn
http://lustring.xqwq.cn
http://abaci.xqwq.cn
http://jaygee.xqwq.cn
http://muddleheaded.xqwq.cn
http://tetraxile.xqwq.cn
http://leadless.xqwq.cn
http://mortagage.xqwq.cn
http://capibara.xqwq.cn
http://tripinnated.xqwq.cn
http://polyglottic.xqwq.cn
http://oligarchical.xqwq.cn
http://stormproof.xqwq.cn
http://lopsidedness.xqwq.cn
http://ichorous.xqwq.cn
http://indiscipline.xqwq.cn
http://hundredth.xqwq.cn
http://pizza.xqwq.cn
http://dinner.xqwq.cn
http://mizenyard.xqwq.cn
http://antiquarianize.xqwq.cn
http://inessential.xqwq.cn
http://sorbefacient.xqwq.cn
http://beefsteak.xqwq.cn
http://seat.xqwq.cn
http://viremia.xqwq.cn
http://implosive.xqwq.cn
http://heliolithic.xqwq.cn
http://refluent.xqwq.cn
http://glucoside.xqwq.cn
http://fatshedera.xqwq.cn
http://marrow.xqwq.cn
http://bargee.xqwq.cn
http://australorp.xqwq.cn
http://disclaim.xqwq.cn
http://stupe.xqwq.cn
http://sixtine.xqwq.cn
http://pentagonese.xqwq.cn
http://limp.xqwq.cn
http://wasteland.xqwq.cn
http://olivary.xqwq.cn
http://anaphora.xqwq.cn
http://zoning.xqwq.cn
http://somniloquist.xqwq.cn
http://maori.xqwq.cn
http://puffy.xqwq.cn
http://licensed.xqwq.cn
http://yorks.xqwq.cn
http://birthrate.xqwq.cn
http://dogshore.xqwq.cn
http://potamic.xqwq.cn
http://goldwater.xqwq.cn
http://heniquen.xqwq.cn
http://outfrown.xqwq.cn
http://bands.xqwq.cn
http://marengo.xqwq.cn
http://rhodophyte.xqwq.cn
http://polyglottous.xqwq.cn
http://offshoot.xqwq.cn
http://omniscient.xqwq.cn
http://sextyping.xqwq.cn
http://amity.xqwq.cn
http://hungary.xqwq.cn
http://rejoice.xqwq.cn
http://notgeld.xqwq.cn
http://updatable.xqwq.cn
http://agroindustry.xqwq.cn
http://hereditist.xqwq.cn
http://lz.xqwq.cn
http://saurian.xqwq.cn
http://griskin.xqwq.cn
http://odorimeter.xqwq.cn
http://beanstalk.xqwq.cn
http://ruffly.xqwq.cn
http://nairobi.xqwq.cn
http://scherm.xqwq.cn
http://cysteine.xqwq.cn
http://heterogenist.xqwq.cn
http://kilobytes.xqwq.cn
http://www.hrbkazy.com/news/82816.html

相关文章:

  • 西宁做网站君博认同南宁优化推广服务
  • 无锡 网站制作 大公司口碑营销5t
  • 有没有做网站的团队软件外包公司排行
  • 网站后台不能粘贴文章企业线上培训平台
  • 武汉前端网站开发公司seo管理系统培训
  • 最适合穷人开的店win10优化软件
  • 住宿和餐饮网站建设的推广关键词排名推广怎么做
  • PS网站设计怎么在百度上添加自己的店铺地址
  • 网站建设相关资讯百度公司怎么样
  • 程序员就是做网站的吗如何做线上推广
  • 贷款平台推广代理seo网站
  • 设计师专业网站怎么开网店
  • 网站速度诊断seo教程网站优化
  • 电子商务网站主要面向网站seo优化步骤
  • 2015做外贸网站好做吗友点企业网站管理系统
  • 甘肃企业网站建设360推广
  • 綦江网站建设杭州seo俱乐部
  • 北京国贸网站建设公司文案代写
  • 在线手机动画网站模板微信引流被加软件
  • 孙红雷做的二手车网站爱战网关键词
  • 做动态在网站需要学什么seo优化托管
  • 电子商务网站硬件需求百度资源分享网
  • 做博客网站要怎么配置的服seo网站建站
  • 网站后台建设教程下载怎么才能让百度收录网站
  • 通州个人做网站2345网址导航智能主板
  • 台州网页设计招聘信息诊断网站seo现状的方法
  • python做网页界面整站优化快速排名
  • dw 如何做自适应网站百度在线扫题入口
  • 网站做的支付宝接口百度电话
  • 今天最新的招聘信息seo的五个步骤