• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Stop a thread

 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using Executors.newFixedThreadPool(5) to create Thread pool. and then use ExecutorService.submit(Callable<> to start a thread out of that.
Now my question is - once one thread has completed its task, how can we kill that thread in this scenario.


Abra D.
 
Henry Wong
author
Sheriff
Posts: 22518
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the callable task is finished, the thread used for the task is returned to the threadpool to execute more tasks. These threads stay alive, waiting for more work, until you shutdown() the executor.

Henry
[ April 25, 2008: Message edited by: Henry Wong ]
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, you just let the thread finish its work and ignore it. The Garbage collector will clean up after you.

Java threads are designed to be cheap.
 
Henry Wong
author
Sheriff
Posts: 22518
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pat Farrell:
Normally, you just let the thread finish its work and ignore it. The Garbage collector will clean up after you.

Java threads are designed to be cheap.



While I normally agree here, keep in mind that the Original Poster is talking about executors. Executors need to be shutdown. If you simply dereference the executor, the GC won't be able to clean up after it.

Henry
 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for informative responses.

Abra D.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!