Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to destroy thread in java after the completion of task

I am using a thread pool for my task. After completion of each task I am destroying the thread using Thread.stop() and Thread.destroy(). But after running my application (in Eclipse) for around 30 min. I am getting a Memory out of bound error.

Please suggest me how to kill the thread.

like image 347
user149621 Avatar asked Dec 03 '09 06:12

user149621


1 Answers

If you're using a thread pool, you shouldn't be terminating the thread to start with - the whole point of a thread pool is to reuse threads.

If you don't want to reuse the thread, then just start a new thread instead of using a thread pool - and just let the thread die, instead of calling stop or destroy. These methods are deprecated for good reason - they basically shouldn't be called.

It's not really clear how this would cause an out of memory exception though - is there any reason why you're focusing on threading as the probable cause?

like image 56
Jon Skeet Avatar answered Sep 21 '22 16:09

Jon Skeet