您的位置:首页 > 汽车 > 新车 > 成品ppt的网站免费直播有哪些_好看的模板_小吴seo博客_百度搜索排名

成品ppt的网站免费直播有哪些_好看的模板_小吴seo博客_百度搜索排名

2025/4/17 22:41:01 来源:https://blog.csdn.net/weixin_44978801/article/details/146359662  浏览:    关键词:成品ppt的网站免费直播有哪些_好看的模板_小吴seo博客_百度搜索排名
成品ppt的网站免费直播有哪些_好看的模板_小吴seo博客_百度搜索排名

Future模式与CompletableFuture

处理异步任务时,FutureCompletableFuture是强有力的工具。

实战案例:多API并行调用

假设我们需要从多个微服务获取数据,然后合并结果:

public UserProfileDto getUserProfile(Long userId) {long startTime = System.currentTimeMillis();// 并行获取用户基本信息CompletableFuture<UserBasicInfo> basicInfoFuture = CompletableFuture.supplyAsync(() -> userService.getBasicInfo(userId));// 并行获取用户订单信息CompletableFuture<List<Order>> ordersFuture = CompletableFuture.supplyAsync(() -> orderService.getUserOrders(userId));// 并行获取用户积分信息CompletableFuture<PointsInfo> pointsFuture = CompletableFuture.supplyAsync(() -> pointsService.getUserPoints(userId));// 等待所有任务完成并合并结果UserProfileDto result = CompletableFuture.allOf(basicInfoFuture, ordersFuture, pointsFuture).thenApply(v -> {UserBasicInfo basicInfo = basicInfoFuture.join();List<Order> orders = ordersFuture.join();PointsInfo points = pointsFuture.join();return new UserProfileDto(basicInfo, orders, points);}).join();long endTime = System.currentTimeMillis();log.info("获取用户档案总耗时: {}ms", (endTime - startTime));return result;
}

使用CompletableFuture可以将原本串行执行的三个服务调用并行化,显著提升响应速度。

线程池的正确使用姿势

线程池是Java并发编程的重要组件,但使用不当会导致严重问题。

线程池核心参数详解

ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize,      // 核心线程数maximumPoolSize,   // 最大线程数keepAliveTime,     // 空闲线程存活时间timeUnit,          // 时间单位workQueue,         // 工作队列threadFactory,     // 线程工厂rejectedExecutionHandler  // 拒绝策略
);

真实踩坑:线程池参数配置不当

某支付系统中,开发人员使用了这样的线程池:

// 错误示范
ExecutorService executor = Executors.newFixedThreadPool(10);

在高峰期,系统大量请求堆积,导致内存溢出。原因是newFixedThreadPool使用的是无界队列LinkedBlockingQueue,请求不断堆积最终耗尽内存。

正确的做法是明确指定队列大小,并设置合适的拒绝策略:

int corePoolSize = 10;
int maximumPoolSize = 20;
long keepAliveTime = 60L;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1000);
ThreadFactory threadFactory = new CustomThreadFactory("payment-thread-");
RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();ExecutorService executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);

这个配置限制了队列大小,并使用CallerRunsPolicy作为拒绝策略,在系统过载时让调用者线程执行任务,起到限流作用。


总结

点赞关注「佩奇的技术笔记」,获取更多并发编程实战技巧!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com