Delaying a thread using the scheduler or Thread.Sleep
In my application, I am making a call to a third-party provider's web service. I need to defer the processing of threads to achieve the required bandwidth supported by the provider's webservice.
I have two options 1. Use Thread.Sleep 2.use ScheduledThreadPoolExecutor as mentioned in the post How to start a thread after a specified time delay in java
We want to know which option is better as we send time critical information (text message) using the Vendor web service. Any help is appreciated.
source to share
They are pretty much the same as the ScheduledThreadPoolExecutor.scheduleWithFixedDelay
sleep call encapsulates.
Since the latency is 100ms, the performance difference is negligible. I would go with ScheduledThreadPoolExecutor.scheduleWithFixedDelay
because of the pooled streams. The amount of load imposed on the system will be manageable, you won't have multiple threads waking up from sleep to compete for resources.
Also from doc
Thread pools address two different problems: they usually provide improved performance when running a large number of asynchronous tasks because of the reduced overhead per task, and they provide a means of limiting and controlling the resources, including threads, consumed when executing a set of tasks. Each ThreadPoolExecutor also maintains some basic statistics such as the number of completed tasks.
source to share
I don't know if it should be called a duplicate of the method that processes the M-queries method in N seconds or not. Queston is different, but the answer may be the same.
source to share
use the scheduler method, you can choose fixed or fixed delay. look at the source code:
/**
* Period in nanoseconds for repeating tasks. A positive
* value indicates fixed-rate execution. A negative value
* indicates fixed-delay execution. A value of 0 indicates a
* non-repeating task.
*/
private final long period;
source to share