views:

68

answers:

3

while creating a thread in java, there is two ways such as Extending threads and Implement runnable Interface. I am unaware of Which is the better way of creating threads?

+1  A: 

Implementing Runnable is better. Josh Bloch covered it in correspondent chapter of Effective Java.

Roman
Wouldn't hurt to list the top motivations here, rather than having the poster follow this advice blindly (even as much as I agree with the book)
Tim
Roman
+2  A: 

Implementing the interface is considered better, but not because of anything specific to threads. In general implementing an interface gives your code more flexibility because you can implement multiple interfaces but only extend a single class. Suppose you wanted to extend another super class and create a thread at the same time?

Vincent Ramdhanie
A: 

Obvously, implementing Runnable is by far better since it potentially allow you to use thread pools and execution queue that you couldn't use with Thread, besides the obvious fact your thread number is limited in the JVM.

Riduidel