I am playing around with Threads. I understand that, we can create threads by two methods 1. by Extending Thread class 2. Implementing Runnable. But, I just want to know, when to which method? I tried to find out in some books, I am referring and JLS also... But, Could not get any clear idea about about choosing the right method.. Can anyone plz exaplain... thanks
It is easier extending Thread than implementing Runnable and then creating a new Thread. Thus use the first method whenever possible. That is whenever your class is already extending of another you can't go for the first method.
Hi Murthy, There is only one way to create thread. That is by creating a Thread object ( or its subclass ). Even if you use a Runnable object you still need a Thread object. In this case you delegate what should done in Thread's run() method to the run() method of that Runnable object. This achieves some flexibility ( A thread's executing code doesn't have to be inside a Thread's object but can be anywhere else! )