package com.sleep.demo; import org.apache.commons.lang3.StringUtils; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class MyThread { private static final int THREAD_SIZE = Integer.parseInt(System.getProperty("getRealPriceInfo.THREAD_SIZE", "20")); private static final long DEFAULT_TIMEOUT = Integer.parseInt(System.getProperty("getRealPriceInfo.defaultTimeout", "10000")); private static final ThreadPoolExecutor REAL_PRICE_EXECUTOR = new ThreadPoolExecutor( THREAD_SIZE, THREAD_SIZE, 30, TimeUnit.MINUTES, new LinkedBlockingDeque<>(), new ThreadFactory() { private final AtomicInteger count = new AtomicInteger(0); @Override public Thread newThread( Runnable r) { Thread thread = new Thread(r); thread.setDaemon(true); thread.setPriority(Thread.NORM_PRIORITY); thread.setName("getRealPriceInfo-" + count.incrementAndGet()); return thread; } }, new ThreadPoolExecutor.CallerRunsPolicy()); public static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity) { List<List<T>> result = new ArrayList<List<T>>(); if (source != null) { int size = source.size(); if (size > 0) { for (int i = 0; i < size; ) { List<T> value = null; int end = i + capacity; if (end > size) { end = size; } value = source.subList(i, end); i = end; result.add(value); } } else { result = null; } } else { result = null; } return result; } public static void main(String[] args) { String skuIds = "1,2,3,4,5,6,7,8,9,10"; String address="ddd"; Map<String, Map<String, String>> returnMap = new HashMap<>(); List<Long> skuList = Arrays.asList(skuIds.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()); List<List<Long>> lists = splitListBycapacity(skuList, 3); List<String> splitGroup = new ArrayList<>(); for (List<Long> list : lists) { String group = StringUtils.join(list, ","); splitGroup.add(group); } List<CompletableFuture<Map<String, Map<String, String>>>> futures = splitGroup.stream() .map(skuIdString -> CompletableFuture.supplyAsync(() -> { try { return queryRealPriceInfo3333(skuIdString, address); } catch (Exception e) { return null; } }, REAL_PRICE_EXECUTOR)) .collect(Collectors.toList()); try { CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) .get(DEFAULT_TIMEOUT, TimeUnit.SECONDS); } catch (Exception e) { throw new RuntimeException("thread pool execute timeout", e); } for (CompletableFuture<Map<String, Map<String, String>>> future : futures) { Map<String, Map<String, String>> now = future.getNow(null); returnMap.putAll(now); } System.out.println(returnMap); } private static Map<String, Map<String, String>> queryRealPriceInfo3333(String skuIds, String address) { List<Long> skuList = Arrays.asList(skuIds.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()); Map<String, Map<String, String>> returnMap = new HashMap<>(); for (Long sku : skuList) { Map<String, String> chiled1 = new HashMap<>(); chiled1.put("a1","a1"); chiled1.put("b1","b1"); chiled1.put("c1","c1"); returnMap.put(sku+"",chiled1); } System.out.println(returnMap); return returnMap; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix