Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt regarding ThreadPoolExecutor  RSS feed

 
Ravinder Rana
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Just want to know if ThreadPoolExecutor.execute() method ensures that if a idle thread is available in thread-pool or we have not reached the maximum thread limit yet, it will execute the submitted task immediately?

Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, there are many different ExecutorService implementations, and there is no general guarantee that would apply to all of them. Since it's an interface, I could make my own implementation of it that may behave differently than those that Sun makes. Second, as soon as we're talking about threads, it's impossible to guarantee that anything will happen immediately. Even if the thread is started immediately, as long as there are other threads running, we can't guarantee how soon it will be before the new thread starts. Third, in all likelihood yes, the new Runnable will be assigned a pooled thread right away, and will start executing as quickly as any thread might be expected to execute. Which is generally pretty fast unless you have too many threads. I don't really see a way to answer this more specifically. You can always run some tests for yourself, logging times using System.currentTimeMillis() to see how much time elapses between calling execute() and calling run().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!