| Author |
Executors
|
Tal Goldstein
Greenhorn
Joined: Apr 16, 2006
Posts: 14
|
|
hi i have a few Runnable's which i would like to start at the same time. what Executor should i use?
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
The Executor is good at managing the threads executing tasks when the number of tasks is higher than the number of threads. For example, if I make a fixed sized pool with 5 threads and throw 100 tasks at it, it will run "5 at once" and as each one finishes it will start a new one, so it will stay loaded at "5 at once" until it gets to the last 4 or so. Other executors can grow and shrink the pool, up to a limit or without limits. There are lots of choices. But if I had 5 tasks and I thought 5 threads would be OK, I'd just start each one on a thread and forget the executor. Any of those things sound like your situation?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Tal Goldstein
Greenhorn
Joined: Apr 16, 2006
Posts: 14
|
|
thanks i read a bit more about executors, and i think in my case it would be right to use an executor with a cached thread pool.
|
 |
Val�ry Urbain
Greenhorn
Joined: Oct 21, 2007
Posts: 11
|
|
Originally posted by Stan James: The Executor is good at managing the threads executing tasks when the number of tasks is higher than the number of threads. For example, if I make a fixed sized pool with 5 threads and throw 100 tasks at it, it will run "5 at once" and as each one finishes it will start a new one, so it will stay loaded at "5 at once" until it gets to the last 4 or so. Other executors can grow and shrink the pool, up to a limit or without limits. There are lots of choices. But if I had 5 tasks and I thought 5 threads would be OK, I'd just start each one on a thread and forget the executor.
Not quite. The Executor javadocs say: An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads. This means I can use whichever execution policy I like. Look at these examples: or or even (your case) use a ThreadPoolExecutor a type of ExecutorService that executes each submitted task using one of possibly several pooled threads.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Hmmm, I don't see where we disagreed, unless you read me as saying the Executor interface useful only when the number of tasks is higher than the number of threads and not in any other situation. The other implementations you showed could be neat when we need to make an application tunable with different strategies for different conditions.
|
 |
 |
|
|
subject: Executors
|
|
|