• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Thread pools work?  RSS feed

 
Pushker Chaubey
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experts,

I wonder how thread pools work. Since a particular thread instance can be given only one runnable target (only once through constructor) and run method runs only once, then how can we reuse same instance to run multiple runnable targets.

Or a pooled thread maintains an infinite loop and runs one runnable target in one iteration....a wild vague thought..

What's the real picture. How does it work?

regards,
Pushker haubey
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pushker,

I'm not really an expert on this topic but although it's probably better not to make too many assumptions about implementation details my guess would be that there's usually a pool of threads maintained and the tasks (~ Runnables) are held in some kind of queue and then executed within those worker threads. Because these threads already run in the background I think it doesn't matter if they call the run() method like any other method. So there's no need to create a new thread for each Runnable which is basically the whole point of thread pools to avoid the overhead of creating new threads.

But that's just a guess, too

Marco
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread can call the Runnable's run() and when that returns wait(), in an infinite loop, and when it is assigned a new Runnable, be notify()-ed...
 
Pushker Chaubey
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for input guys!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!