使用调度程序或Thread.Sleep的线程延迟 [英] Thread Delay using scheduler or Thread.Sleep

查看:125
本文介绍了使用调度程序或Thread.Sleep的线程延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application I am calling third part vendor web-service. I need to delay my thread processing to achieve required throughput supported by vendor webservice.

我有两个选择1.使用Thread.Sleep2.使用帖子 How中提到的ScheduledThreadPoolExecutor在Java中指定的时间延迟后启动线程

I have two options 1. Use Thread.Sleep 2. use ScheduledThreadPoolExecutor as mentioned in the post How to start a thread after specified time delay in java

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

Wanted to know which is better option as we are sending time critical information(Text Message) using Vendor webservice. Any help is appreciated.

推荐答案

它们与 ScheduledThreadPoolExecutor.scheduleWithFixedDelay 封装了睡眠调用几乎相同.

They're pretty much the same as ScheduledThreadPoolExecutor.scheduleWithFixedDelay encapsulates the sleep call.

由于延迟为100ms,因此性能差异可以忽略不计.由于线程池多,我会选择 ScheduledThreadPoolExecutor.scheduleWithFixedDelay .施加在系统上的负载量是可管理的,您不会有多个线程从睡眠中唤醒来竞争资源.

Since the delay is 100ms performance difference is kind of negligible. I'd go with ScheduledThreadPoolExecutor.scheduleWithFixedDelay due to pooled threads. The amount of load put on the system would be manageable, you wouldn't have multiple threads waking up from sleep together to compete for resources.

也来自 doc

线程池解决了两个不同的问题:它们通常提供在执行大量异步操作时提高了性能任务,因为减少了每个任务的调用开销,并且它们提供了限制和管理资源(包括线程)的方法,在执行任务集合时消耗.每个ThreadPoolExecutor还维护一些基本统计信息,例如完成的数量任务.

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.

这篇关于使用调度程序或Thread.Sleep的线程延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆