五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

CompletableFuture實(shí)現(xiàn)異步并阻塞獲取返回結(jié)果,巧用CompletableFuture返回值解決性能

2022-12-25 11:14 作者:萬事俱備就差個程序員  | 我要投稿

CompletableFuture實(shí)現(xiàn)異步并阻塞獲取返回結(jié)果,巧用CompletableFuture返回值解決性能瓶頸,線程池,異步編排


參考: https://blog.csdn.net/LUOHUAPINGWIN/article/details/122222011

? ? ? https://blog.csdn.net/sunquan291/article/details/103991184

??

??

配置:?

gulimall.thread.coreSize=20

gulimall.thread.maxSize=200

gulimall.thread.keepAliveTime=10


讀取配置:

package com.xunqi.gulimall.order.config;


import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;


/**

?* @Description:

?* @Created: with IntelliJ IDEA.

?* @author: 夏沫止水

?* @createTime: 2020-06-23 20:28

?**/


@ConfigurationProperties(prefix = "gulimall.thread")

// @Component

@Data

public class ThreadPoolConfigProperties {


? ? private Integer coreSize;


? ? private Integer maxSize;


? ? private Integer keepAliveTime;



}



注入線程池:

package com.xunqi.gulimall.order.config;


import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


import java.util.concurrent.Executors;

import java.util.concurrent.LinkedBlockingDeque;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;


/**

?* @Description: 線程池配置類

?* @Created: with IntelliJ IDEA.

?* @author: 夏沫止水

?* @createTime: 2020-06-23 20:24

?**/


@EnableConfigurationProperties(ThreadPoolConfigProperties.class)

@Configuration

public class MyThreadConfig {



? ? @Bean

? ? public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {

? ? ? ? return new ThreadPoolExecutor(

? ? ? ? ? ? ? ? pool.getCoreSize(),

? ? ? ? ? ? ? ? pool.getMaxSize(),

? ? ? ? ? ? ? ? pool.getKeepAliveTime(),

? ? ? ? ? ? ? ? TimeUnit.SECONDS,

? ? ? ? ? ? ? ? new LinkedBlockingDeque<>(100000),

? ? ? ? ? ? ? ? Executors.defaultThreadFactory(),

? ? ? ? ? ? ? ? new ThreadPoolExecutor.AbortPolicy()

? ? ? ? );

? ? }


}



使用:

? ? @Autowired

? ? private ThreadPoolExecutor threadPoolExecutor;

@Override

? ? public List<WxUserInfo> getWxUserInfoByUid(String appid, List<Long> uidList) {

? ? ? ? // 數(shù)據(jù)太多了.分片執(zhí)行

? ? ? ? List<List<Long>> uidListGroupList = CollectionUtil.split(uidList, 500);


? ? ? ? List<CompletableFuture<List<WxUserInfo>>> futures = uidListGroupList.stream().map(list -> {

? ? ? ? ? ? return CompletableFuture.supplyAsync(() -> {

? ? ? ? ? ? ? ? RestResult<List<WxUserInfo>> wxUserInfoByAppIdUid = passportFeignService.getWxUserInfoByAppIdUid(appid, list, appName);

? ? ? ? ? ? ? ? return wxUserInfoByAppIdUid.getData();

? ? ? ? ? ? }, threadPoolExecutor);

? ? ? ? }).collect(Collectors.toList());


? ? ? ? // List<WxUserInfo> collect = futures.stream().map(p -> {

? ? ? ? //? ? ?try {

? ? ? ? //? ? ? ? ?return p.get();

? ? ? ? //? ? ?} catch (InterruptedException e) {

? ? ? ? //? ? ? ? ?e.printStackTrace();

? ? ? ? //? ? ?} catch (ExecutionException e) {

? ? ? ? //? ? ? ? ?e.printStackTrace();

? ? ? ? //? ? ?}

? ? ? ? //? ? ?return null;

? ? ? ? // }).filter(Objects::nonNull).flatMap(List::stream).collect(Collectors.toList());


? ? ? ? List<WxUserInfo> biddingList = futures.stream().map(CompletableFuture::join).filter(Objects::nonNull).flatMap(List::stream).collect(Collectors.toList());

? ? ? ? return biddingList;

? ? }


CompletableFuture實(shí)現(xiàn)異步并阻塞獲取返回結(jié)果,巧用CompletableFuture返回值解決性能的評論 (共 條)

分享到微博請遵守國家法律
乌恰县| 福贡县| 招远市| 大安市| 保山市| 隆子县| 陇川县| 德清县| 郯城县| 新丰县| 西平县| 襄樊市| 万载县| 长葛市| 平泉县| 柯坪县| 大英县| 平顶山市| 定陶县| 资溪县| 阳泉市| 屏东县| 盐边县| 兰溪市| 连南| 营山县| 武城县| 东光县| 康马县| 阳泉市| 海城市| 彝良县| 舟山市| 乐东| 东平县| 张北县| 日土县| 永嘉县| 嵩明县| 吉林市| 龙川县|