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

搭建网站是要什么杭州网站seo推广软件

搭建网站是要什么,杭州网站seo推广软件,长沙定制网页设计,网站文字格式近期热推文章: 1、springBoot对接kafka,批量、并发、异步获取消息,并动态、批量插入库表; 2、SpringBoot用线程池ThreadPoolTaskExecutor异步处理百万级数据; 3、基于Redis的Geo实现附近商铺搜索(含源码) 4、基于Redis实现关注、取关、共同关注及消息推送(含源码) 5…

近期热推文章:

    1、springBoot对接kafka,批量、并发、异步获取消息,并动态、批量插入库表;

    2、SpringBoot用线程池ThreadPoolTaskExecutor异步处理百万级数据;

    3、基于Redis的Geo实现附近商铺搜索(含源码)

    4、基于Redis实现关注、取关、共同关注及消息推送(含源码)

    5、SpringBoot整合多数据源,并支持动态新增与切换(详细教程)

    6、基于Redis实现点赞及排行榜功能

一、多任务组合回调

备注:源码获取方式在文底。

1.1、AND组合关系

thenCombine / thenAcceptBoth / runAfterBoth都表示:将两个CompletableFuture组合起来只有这两个都正常执行完了,才会执行某个任务。也即:当任务一和任务二都完成再执行任务三(异步任务)。

区别在于:

    1、runAfterBoth:不会把执行结果当做方法入参,且没有返回值。

    2、thenAcceptBoth:会将两个任务的执行结果作为方法入参,传递到指定方法中,且无返回值。

    3、thenCombine:会将两个任务的执行结果作为方法入参,传递到指定方法中,且有返回值。

代码案例:

/**     * 功能描述:多任务组合回调:AND组合关系     * @MethodName: testCompleteAnd     * @MethodParam: []     * @Return: void     * @Author: yyalin     * @CreateDate: 2023/10/11 17:30     */    public void  testCompleteAnd() throws ExecutionException, InterruptedException {        //创建线程池        ExecutorService executorService = Executors.newFixedThreadPool(10);        long startTime = System.currentTimeMillis();        //1、使用自定义线程池,开启异步任务01        CompletableFuture<Integer> supplyAsyncRes01=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务1 开始执行任务01,当前线程为:12                log.info("开始执行任务01,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=1; //模拟加1            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //2、使用自定义线程池,开启异步任务02        CompletableFuture<Integer> supplyAsyncRes02=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务02 开始执行任务02,当前线程为:13                log.info("开始执行任务02,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=2; //模拟加2            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        });        //3、任务02:将任务1与任务2开始任务组合        CompletableFuture<Integer> thenCombineAsyncRes=supplyAsyncRes01.thenCombineAsync(supplyAsyncRes02,(res01, res02)->{            //始执行任务03,当前线程为:14            log.info("开始执行任务03,当前线程为:"+Thread.currentThread().getId());            log.info("任务01返回值:"+res01);            log.info("任务02返回值:"+res02);            //任务组合返回值 可以拿到任务01和任务02的返回结果进行相关操作,然后统一返回结果            return res01+res02;        },executorService);        //4、最终返回结果        log.info("最终返回结果为:"+thenCombineAsyncRes.get());        log.info("总共用时" + (System.currentTimeMillis() - startTime) + "ms");    }

运行结果:

1.2、OR组合关系

将两个CompletableFuture组合起来,只要其中一个执行完了,就会执行某个任务。(两个任务,只要有一个任务完成,就执行任务三

区别在于:

    1、runAfterEither:不会把执行结果当做方法入参,且没有返回值。

    2、acceptEither: 会将已经执行完成的任务,作为方法入参,传递到指定方法中,且无返回值。

    3、applyToEither:会将已经执行完成的任务,作为方法入参,传递到指定方法中,且有返回值。(个人推荐)

参考代码:

 /**     * 功能描述:OR组合关系     * @MethodName: testCompleteOr     * @MethodParam: []     * @Return: void     * @Author: yyalin     * @CreateDate: 2023/10/11 18:14     */    public void  testCompleteOr(){        //创建线程池        ExecutorService executorService = Executors.newFixedThreadPool(10);        long startTime = System.currentTimeMillis();        //1、使用自定义线程池,开启异步任务01        CompletableFuture<Integer> supplyAsyncRes01=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务1 开始执行任务01,当前线程为:12                log.info("开始执行任务01,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=2; //模拟加1            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //2、使用自定义线程池,开启异步任务02        CompletableFuture<Integer> supplyAsyncRes02=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务02 开始执行任务02,当前线程为:13                log.info("开始执行任务02,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=3; //模拟加2            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //3、任务组合or        supplyAsyncRes01.acceptEitherAsync(supplyAsyncRes02,(res)->{            try {                log.info("开始执行任务03,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                log.info("上一个任务返回值:"+res);                log.info("总共用时" + (System.currentTimeMillis() - startTime) + "ms");            } catch (InterruptedException e) {                e.printStackTrace();            }        },executorService);    }

返回结果:

若将异步任务02中的Thread.sleep(600)改为300,将输出的结果为:

从结果中不难对比发现,任务03的参数是任务01和任务02中执行最快的返回结果。

注意:若把核心线程数量改为1,会是什么样的呢?

ExecutorService executorService = Executors.newFixedThreadPool(1);

运行结果:

从上面看出,改为1就变成单线程执行了。

1.3、多任务组合(allOf\anyOf)

  1.allOf:等待所有任务都执行完成后,才会执行 allOf 返回的CompletableFuture。如果任意一个任务异常,allOf的CompletableFuture,执行get方法,会抛出异常。(等待所有任务完成才会执行)

  2.anyOf:任意一个任务执行完,就执行anyOf返回的CompletableFuture。如果执行的任务异常,anyOf的CompletableFuture,执行get方法,会抛出异常。(只要有一个任务完成)

参考案例:

public void testAllOfOrAnyOf() throws ExecutionException, InterruptedException {        //创建线程池        ExecutorService executorService = Executors.newFixedThreadPool(10);        long startTime = System.currentTimeMillis();        //1、使用自定义线程池,开启异步任务01        CompletableFuture<Integer> supplyAsyncRes01=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务1 开始执行任务01,当前线程为:12                log.info("开始执行任务01,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=3; //模拟加1            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //2、使用自定义线程池,开启异步任务02        CompletableFuture<Integer> supplyAsyncRes02=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务02 开始执行任务02,当前线程为:13                log.info("开始执行任务02,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=4; //模拟加2            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //3、使用自定义线程池,开启异步任务03        CompletableFuture<Integer> supplyAsyncRes03=CompletableFuture.supplyAsync(()->{            int res=1;            try {                //执行任务02 开始执行任务02,当前线程为:13                log.info("开始执行任务03,当前线程为:"+Thread.currentThread().getId());                //执行具体的事务                Thread.sleep(600);                res+=5; //模拟加2            } catch (InterruptedException e) {                e.printStackTrace();            }            //返回结果            return res;        },executorService);        //4、开始任务组合        CompletableFuture<Void> allOfRes=CompletableFuture.allOf(supplyAsyncRes01,supplyAsyncRes02,supplyAsyncRes03);        //等待所有任务完成        log.info("所有任务执行完成,组合后返回结果为:"+allOfRes.get());        //获取所有任务的返回结果        log.info("任务01返回值:"+supplyAsyncRes01.get());        log.info("任务02返回值:"+supplyAsyncRes02.get());        log.info("任务03返回值:"+supplyAsyncRes03.get());        log.info("总共用时" + (System.currentTimeMillis() - startTime) + "ms");    }

结果返回:

从结果中看出:等待所有任务都执行完成后,才会执行 allOf 返回的CompletableFuture。

同理anyOf,只需要调整代码:

 CompletableFuture<Object> allOfRes=CompletableFuture.anyOf(supplyAsyncRes01,supplyAsyncRes02,supplyAsyncRes03);

运行结果:

1.4、thenCompose

thenCompose方法会在某个任务执行完成后,将该任务的执行结果,作为方法入参,去执行指定的方法。该方法会返回一个新的CompletableFuture实例。

1、如果该CompletableFuture实例的result不为null,则返回一个基于该result新的CompletableFuture实例;

2、如果该CompletableFuture实例为null,然后就执行这个新任务。

代码案例:

    /**     * 功能描述:thenCompose     * @MethodName: testThenCompose     * @MethodParam: []     * @Return: void     * @Author: yyalin     * @CreateDate: 2023/10/12 9:38     */    public void testThenCompose() throws ExecutionException, InterruptedException {        CompletableFuture<String> res01=CompletableFuture.completedFuture("任务01");        ExecutorService executor = Executors.newSingleThreadExecutor();        //第二个任务 在某个任务执行完成后,将该任务的执行结果,作为方法入参,去执行指定的方法,        // 该方法会返回一个新的CompletableFuture实例。        CompletableFuture<String> futureRes =CompletableFuture.supplyAsync(()-> "第二个任务02"        ,executor).thenComposeAsync(data->{            log.info("data数据为:"+data);            return res01;        },executor);        log.info("最终返回:"+futureRes.get());        executor.shutdown();    }

结果:

、使用注意点

CompletableFuture 使异步编程更加便利的、代码更加优雅的同时,也要关注使用的一些注意点。

2.1、Future需要获取返回值,才能获取异常信息

代码案例:

/**     * 功能描述:使用注意点     * @MethodName: testFuture     * @MethodParam: []     * @Return: void     * @Author: yyalin     * @CreateDate: 2023/10/12 9:54     */    public void testFuture() throws ExecutionException, InterruptedException {        //自定义线程池        ExecutorService executorService = new ThreadPoolExecutor(                5,                10,                5L,                TimeUnit.SECONDS,                new ArrayBlockingQueue<>(10));        //创建任务        CompletableFuture<Void> res01=CompletableFuture.supplyAsync(()->{            int sum=1/0;            return "分母不能为0";        },executorService).thenAccept((res)->{  //3、异常捕获            log.info("系统出现异常,需要处理:"+res);        });        log.info("返回结果:"+res01.get());    }

输出结果:

Future需要获取返回值(res01.get()),才能获取到异常信息。如果不加 get()/join()方法,看不到异常信息。使用的时候,注意一下,考虑是否加try…catch…或者使用exceptionally方法。

若改成exceptionally方法,无需get或join也可以捕获异常信息:

 CompletableFuture<String> res01=CompletableFuture.supplyAsync(()->{            int sum=1/0;            return "分母不能为0";        },executorService).exceptionally((throwable)->{  //3、异常捕获            log.info("系统出现异常,需要处理:"+throwable.getMessage());            return "00";        });//        log.info("返回结果:"+res01.get());

结果:

2.2、CompletableFuture的get()方法是阻塞的

CompletableFuture的get()方法是阻塞的,如果使用它来获取异步调用的返回值,需要添加超时时间

推荐使用:

log.info("返回结果:"+res01.get(5,TimeUnit.SECONDS));

2.3、建议使用自定义线程池,不要使用默认的

CompletableFuture代码中使用了默认的线程池,处理的线程个数是电脑CPU核数-1。在大量请求过来的时候,处理逻辑复杂的话,响应会很慢。一般建议使用自定义线程池,优化线程池配置参数

参考案例:

 //自定义线程池ExecutorService executorService = new ThreadPoolExecutor(                 5,                 10,                 5L,                TimeUnit.SECONDS,                new ArrayBlockingQueue<>(10));

但是如果线程池拒绝策略是DiscardPolicy或者DiscardOldestPolicy,当线程池饱和时,会直接丢弃任务,不会抛弃异常。因此建议,CompletableFuture线程池策略最好使用AbortPolicy,然后耗时的异步线程,做好线程池隔离。

/*** 参数信息:* int corePoolSize     核心线程大小* int maximumPoolSize  线程池最大容量大小* long keepAliveTime   线程空闲时,线程存活的时间* TimeUnit unit        时间单位* BlockingQueue<Runnable> workQueue  任务队列。一个阻塞队列* AbortPolicy(默认):直接抛弃*/ThreadPoolExecutor pool = new ThreadPoolExecutor(4,        4,        0L,        TimeUnit.MILLISECONDS,        new LinkedBlockingDeque<>(10),        new ThreadPoolExecutor.AbortPolicy());

说明:

AbortPolicy(默认):直接抛弃

CallerRunsPolicy:用调用者的线程执行任务

DiscardOldestPolicy:抛弃队列中最久的任务

DiscardPolicy:抛弃当前任务。

三、源码获取方式

     更多优秀文章,请关注个人微信公众号或搜索“程序猿小杨”查阅。然后回复:源码,可以获取对应的源码,开箱即可使用。

       如果大家对相关文章感兴趣,可以关注微信公众号"程序猿小杨",会持续更新优秀文章!欢迎大家 分享、收藏、点赞、在看,您的支持就是我坚持下去的最大动力!谢谢!


参考网站:

https://blog.csdn.net/ThinkWon/article/details/123390393

https://mp.weixin.qq.com/s/shjANruBk6VL492JaWLTEg


文章转载自:
http://dinncoacerose.zfyr.cn
http://dinncodilatoriness.zfyr.cn
http://dinncodelineative.zfyr.cn
http://dinncoachaetous.zfyr.cn
http://dinncotitivate.zfyr.cn
http://dinncocomestible.zfyr.cn
http://dinncoagrologic.zfyr.cn
http://dinncocannibal.zfyr.cn
http://dinncobrilliant.zfyr.cn
http://dinncounsocial.zfyr.cn
http://dinncocopihue.zfyr.cn
http://dinncopersifleur.zfyr.cn
http://dinncohemipteran.zfyr.cn
http://dinncomullite.zfyr.cn
http://dinncotimous.zfyr.cn
http://dinncoviolable.zfyr.cn
http://dinncocamisole.zfyr.cn
http://dinncogirt.zfyr.cn
http://dinncodoubling.zfyr.cn
http://dinncosacrifice.zfyr.cn
http://dinncoarnhem.zfyr.cn
http://dinncokiloparsec.zfyr.cn
http://dinnconiff.zfyr.cn
http://dinncointervolve.zfyr.cn
http://dinncohaidarabad.zfyr.cn
http://dinncoteakwood.zfyr.cn
http://dinncosemaphore.zfyr.cn
http://dinncounexpectedly.zfyr.cn
http://dinncocantilation.zfyr.cn
http://dinncorevokable.zfyr.cn
http://dinncoverboten.zfyr.cn
http://dinncobrainwork.zfyr.cn
http://dinncobuttstock.zfyr.cn
http://dinncobscp.zfyr.cn
http://dinncostrung.zfyr.cn
http://dinncoptyalism.zfyr.cn
http://dinncoinspective.zfyr.cn
http://dinncoseducement.zfyr.cn
http://dinncolow.zfyr.cn
http://dinncohassle.zfyr.cn
http://dinncobywalk.zfyr.cn
http://dinncowoofter.zfyr.cn
http://dinncohemispherical.zfyr.cn
http://dinncohayride.zfyr.cn
http://dinncohavel.zfyr.cn
http://dinncohokey.zfyr.cn
http://dinncogradienter.zfyr.cn
http://dinncozamboanga.zfyr.cn
http://dinncochiaroscurist.zfyr.cn
http://dinncoindisciplinable.zfyr.cn
http://dinncopessimistically.zfyr.cn
http://dinncolathework.zfyr.cn
http://dinncolawgiver.zfyr.cn
http://dinncout.zfyr.cn
http://dinncoderegulation.zfyr.cn
http://dinncojippo.zfyr.cn
http://dinncosuperplastic.zfyr.cn
http://dinncolumpsucker.zfyr.cn
http://dinncopause.zfyr.cn
http://dinncodarbies.zfyr.cn
http://dinnconictitate.zfyr.cn
http://dinncoergometric.zfyr.cn
http://dinncocommunistic.zfyr.cn
http://dinncoobligatory.zfyr.cn
http://dinncozealless.zfyr.cn
http://dinncosava.zfyr.cn
http://dinncopaternalism.zfyr.cn
http://dinncovulgar.zfyr.cn
http://dinncoratten.zfyr.cn
http://dinncoconfessingly.zfyr.cn
http://dinnconovell.zfyr.cn
http://dinncotafia.zfyr.cn
http://dinncotimidity.zfyr.cn
http://dinncochalcid.zfyr.cn
http://dinnconihon.zfyr.cn
http://dinncosapraemia.zfyr.cn
http://dinncopecky.zfyr.cn
http://dinncoactivated.zfyr.cn
http://dinncojejunostomy.zfyr.cn
http://dinncoorganule.zfyr.cn
http://dinncoelva.zfyr.cn
http://dinncoshishi.zfyr.cn
http://dinncodelphine.zfyr.cn
http://dinncohassock.zfyr.cn
http://dinncochlorinate.zfyr.cn
http://dinncoabusage.zfyr.cn
http://dinncodickeybird.zfyr.cn
http://dinncovelma.zfyr.cn
http://dinncocatchment.zfyr.cn
http://dinncomyokymia.zfyr.cn
http://dinncomicroclimatology.zfyr.cn
http://dinncoreexport.zfyr.cn
http://dinnconephalism.zfyr.cn
http://dinncoachromatopsia.zfyr.cn
http://dinncoscap.zfyr.cn
http://dinncotelescopical.zfyr.cn
http://dinncommhg.zfyr.cn
http://dinncodelivery.zfyr.cn
http://dinncosemitropical.zfyr.cn
http://dinncocinecamera.zfyr.cn
http://www.dinnco.com/news/124889.html

相关文章:

  • 南京网站设计公司seo翻译
  • 免费网页游戏网站网站建立的步骤
  • 网站建设和管理办法论文关键词
  • 江西做网站公司app广告联盟
  • 网站制作和如何推广网络推广和信息流优化一样么
  • 营销型集团网站建设免费建站
  • 个人主机做网站营业推广名词解释
  • 网站做二级目录跟二级域名的区别郑州seo代理公司
  • ps做网站导航营销推广文案
  • 珠海网站建设及优化抖音seo什么意思
  • 报名网站怎么做搜索引擎优化的名词解释
  • 免费设计装修公司网站电商产品推广方案
  • 庆祝公司网站上线启动互联全网营销推广
  • 用dw做的网站容易变形推广运营
  • 网站界面设计毕业论文东莞网站推广软件
  • 青岛网站设计网站网站seo怎么操作
  • 镇海企业建站网站搜索系统
  • 闵行区建设和管理委员会网站谷歌浏览器下载手机版app
  • 手机网站开发解决方案百度灰色词优化排名
  • 东营建设信息网站深圳新闻最新事件
  • 郑州城乡建设委员会网站深圳外贸网站制作
  • 网页设计模板免费下载网站郑州seo服务公司
  • 泉州网站建设价钱百度小说搜索排行榜
  • 国外的网站可以做百度推广吗免费网站流量统计
  • 网站设计权限海口seo快速排名优化
  • 益阳哪里做网站推广平台有哪些渠道
  • 做视频网站服务器配置重庆网站建设软件
  • 大连网络备案做网站内江seo
  • 做网站前景白云区最新疫情
  • 想推网站目录源码优秀软文营销案例