Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.sleep() VS Executor.scheduleWithFixedDelay()

Goal: Execute certain code every once in a while.

Question: In terms of performance, is there a significant difference between:

while(true) {     execute();     Thread.sleep(10 * 1000); } 

and

executor.scheduleWithFixedDelay(runnableWithoutSleep, 0, 10, TimeUnit.SECONDS); 

?

Of course, the latter option is more kosher. Yet, I would like to know whether I should embark on an adventure called "Spend a few days refactoring legacy code to say goodbye to Thread.sleep()".

Update: This code runs in super/mega/hyper high-load environment.

like image 328
rudgirello Avatar asked Nov 16 '12 18:11

rudgirello


People also ask

What is the difference between thread and Executor?

A Thread represents something which is responsible for executing your code in parallel, while an Executor is an abstraction for concurrent task execution.

What is the difference between a wait () and sleep () in threads?

Sleep() method belongs to Thread class. Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context.

What is Executor in multithreading?

The Executor Framework contains a bunch of components that are used to efficiently manage multiple threads. It was released with the JDK 5 which is used to run the Runnable objects without creating new threads every time and also mostly re-using the already created threads.

What are Executor and ExecutorService and what are the differences between them?

Executor just executes stuff you give it. ExecutorService adds startup, shutdown, and the ability to wait for and look at the status of jobs you've submitted for execution on top of Executor (which it extends). This is a perfect answer, short and clear.


1 Answers

You're dealing with sleep times termed in tens of seconds. The possible savings by changing your sleep option here is likely nanoseconds or microseconds.

I'd prefer the latter style every time, but if you have the former and it's going to cost you a lot to change it, "improving performance" isn't a particularly good justification.

EDIT re: 8000 threads

8000 threads is an awful lot; I might move to the scheduled executor just so that you can control the amount of load put on your system. Your point about varying wakeup times is something to be aware of, although I would argue that the bigger risk is a stampede of threads all sleeping and then waking in close succession and competing for all the system resources.

I would spend the time to throw these all in a fixed thread pool scheduled executor. Only have as many running concurrently as you have available of the most limited resource (for example, # cores, or # IO paths) plus a few to pick up any slop. This will give you good throughput at the expense of latency.

With the Thread.sleep() method it will be very hard to control what is going on, and you will likely lose out on both throughput and latency.

If you need more detailed advice, you'll probably have to describe what you're trying to do in more detail.

like image 194
Steven Schlansker Avatar answered Sep 23 '22 11:09

Steven Schlansker