Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Executors

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic