Some questions about Executors best usage for memory and time performance:
Is there any cost penalty incurred for the use of
ExecutorService e = Executors.newSingleThreadExecutor();
e.execute(callable)
e.shutdown()
compared to:
new Thread(runnable).start()
If a Callable
is not a long one, and never won't be more than one instance running of it it's ok to use the code from (1)? Or is it best to have ExecutorService
as a static instance and reuse between calls?
If I have several tasks as described in (2) is there any problem with every task to having its own executor service or is better to have a centralized one? (I'm talking about a client application where normally there won't be more than one of these tasks running)
What resources does an Executor
consume if it isn't shutdown()?
Is there any cost penalty from using
ExecutorService e=Executors.newSingleThreadExecutor();
e.execute(callable)
e.shutdown()
compared to:
new Thread(runnable).start()
Yes, there is a "penalty": the ExecutorService
will most likely be more expensive to create since it also creates a queue for the tasks you're submitting and if a thread fails prior to shutdown, then the failed thread will be replaced with another one in order to run any subsequent tasks (so there is quite a bit of logic there). However, you probably don't want to be creating an ExecutiveService
each time you want to run a task... that's probably not the best use of an ExecutorService
(more on that in the next question).
If a Callable is not a long one, and never won't be more than one instance running of it it's ok to use the code from -1-? Or it's best to have ExecutorService as static one and reuse between calls?
The suggested use of the ExecutorService
is as a thread pool, where you keep the ExecutorService
around and you keep submitting tasks to it as long as you have tasks to submit. It can be static or just a regular member, that is irrelevant and highly dependent on your requirements/design. Even if you're only running one instance at a time (i.e. you only have a single-threaded executor), it's still more efficient to use the ExecutorService
because it reuses the thread, so in the long run it will be less expensive than creating a new thread for each task you submit.
If I have several task... is there any problem for every task to have their executor service or is better to have a centralized one?
There is no problem, but it's inefficient, so just have a centralized executor service.
What resources consumes an Executor if it isn't shutdown()?
I don't think you should worry about that in particular, especially if you're using the Executor
in the correct context it will be minimal.
The point of an ExecutorService is to share many tasks among a fixed number of threads. Creating an ExecutorService to run a single task is wasteful overhead.
new Thread(runnable).start()
should always be faster than creating an ExecutorService as the ExecutorService will just create the thread anyway, but with the added overhead of keeping track of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With