how to destroy thread in java after the completion of task

java, multithreading

Solution

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?

Problem

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.

Original source