Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java concurrency  RSS feed

 
Steve Chambers
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were to create an executor with a thread pool. Is that thread pool available to the whole application or is a new one instanciated per each method call or class instanciation?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends where you put the code that starts the thread pool, and where you store the reference to that pool. If you use a static variable and static initializer, then the entire application is using the same thread pool. If it's an instance variable initialized in a constructor, then each instance has a different pool. If you use a local variable in a method, then you get a new pool each time you call the method. (This last seems pretty pointless in general, though there may be a few cases where it makes sense.)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense in the run() method of a thread that runs for the life of the application
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!