Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executors

 
Tal Goldstein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tal Goldstein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic