Which is the best practise in threads to implement Runnable interface or extending thread class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which is the best practise in threads to implement Runnable interface or extending thread class?

12th Oct 2016, 5:33 PM
Kunapareddy Vj
Kunapareddy Vj - avatar
3 Answers
+ 3
Hmm, try not to extend the Thread class (or any other class in the standard library for that matter) and implement Runnable instead. However, there is an even cleaner way of doing things: `Future`s and `FutureTask`s. Basically they are functions which you then `.submit()` to a so-called `Executor`, which does all the threading behind the scenes. This way you don't need to create any Threads at all. Check them out some time, they are really neat! If you are using Java 8 they are even nicer to work with (using lambdas).
12th Oct 2016, 6:31 PM
Schindlabua
Schindlabua - avatar
+ 1
Runnable interface is preferable because it keeps your option of inheriting a super class open if needed in future.
13th Oct 2016, 3:36 AM
Akash Middinti
0
That's good from you @Schindlabua
12th Oct 2016, 7:35 PM
THE.GEEK
THE.GEEK - avatar