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

标签 java multithreading delay sleep

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

我有两个选择 1.使用Thread.Sleep 2. 使用帖子中提到的 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/

相关文章:

java - 向java代码添加延迟

java - Eclipse Neon 无法运行

java - 在 Groovy 中使用 Google-Reflection 会导致异常,而等效的 Java 代码可以工作

c++ - 在多个线程中从列表中删除元素

java - 客户端之间无法通信

android - 按下按钮后延迟更新 TextView

java - 来自 Java 应用程序的 Apache cTAKES 逻辑

java - 如何使用 simplewire for Java 将 SMS 从 PC 发送到手机?

c# - 处理源自线程的错误的标准方法

java - 让监听器等待javafx