Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executors use and cost penalty

Some questions about Executors best usage for memory and time performance:

  1. 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()
    
  2. 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?

  3. 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)

  4. What resources does an Executor consume if it isn't shutdown()?

like image 546
lujop Avatar asked Aug 23 '11 20:08

lujop


2 Answers

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.

like image 165
Kiril Avatar answered Oct 16 '22 01:10

Kiril


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.

like image 37
Andrew Avatar answered Oct 16 '22 01:10

Andrew