What is difference between extends Thread and implemenst Runnable in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is difference between extends Thread and implemenst Runnable in Java

What is pros n cons and is there any performance advantage one over other

7th Apr 2017, 3:28 PM
kz kz
kz kz - avatar
1 Answer
+ 1
Runnable is an interface that is used in many cenerious if you have a method that calls an other method given to her. There is thread whose start method calls the runnables run method but also others like SwingUtilities.invokeLater (Runnable run) which runs the runnable on the UI thread causing weird UI bugs to disappear. A thread on the other hand rund a code parallel to ther code. This can be done by overriding its start method or by passing a runnable to it. Performancewise there shouldn't be any big difference except that you create one more object using runnable. Using runnable could turn out to be more flexible if you need to use it often as a runnable can be run multiple times, a thread only once. Also you can basically pass the function you want to call if you use runnable but you want the thread to be encapsulated so noone accidentally calls start. To sum it up, if you want to pass the code you want to call in a threat with your own method or you want to use it multiple times use runnable and pass it to a new thread. If you want to use it only once internally it's personal style preference.
8th Apr 2017, 5:53 AM
No One
No One - avatar