gpt4 book ai didi

java - 使用调度程序或 Thread.Sleep 的线程延迟

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:18 26 4
gpt4 key购买 nike

在我的应用程序中,我调用了第三方供应商网络服务。我需要延迟我的线程处理以实现供应商 Web 服务支持的所需吞吐量。

我有两个选择1.使用Thread.Sleep2. 使用帖子中提到的 ScheduledThreadPoolExecutor How to start a thread after specified time delay in java

想知道哪个是更好的选择,因为我们正在使用供应商网络服务发送时间紧迫的信息(文本消息)。感谢您的帮助。

最佳答案

它们与 ScheduledThreadPoolExecutor.scheduleWithFixedDelay 封装 sleep 调用非常相似。

由于延迟是 100 毫秒,因此性能差异可以忽略不计。由于线程池,我会选择 ScheduledThreadPoolExecutor.scheduleWithFixedDelay。系统上的负载量将是可控的,您不会让多个线程一起从 sleep 中醒来来竞争资源。

同样来自 doc

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

关于java - 使用调度程序或 Thread.Sleep 的线程延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com