Tuesday, June 9, 2009

Reusability of Thread.

Can you resuse a Thread Object? Can you restart it by calling start() again?

No. Once a thread's run() method has completed, the thread can never be restarted. In fact, at that point the thread moves into a state - dead. In dead state, the thead has finished its run()method and can never be restarted. The Thread object might still be on the heap, as aliving object that you can call other methods on (if appropriate), but the Thread object has permanently lost its 'threadness'. In other words, there is no longer a seperate call stack, and the Thread object is no longer a thread. It's Just an object, at that point, like all other objects.

But there are design patterns for making a pool of threads that you can keep using to perform different jobs. But you don't do it by retsarting a dead thread.

No comments:

Post a Comment