Thread vs runnable interface which one is best | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

Thread vs runnable interface which one is best

14th Apr 2017, 4:51 AM
Nikhil
Nikhil - avatar
3 Respostas
+ 10
by extendingĀ Thread, each of your threadsĀ has a unique object associated with it, whereas implementingĀ Runnable, manyĀ thread scan share the same object instance. In theĀ Runnable interfaceĀ approach, only one instance of a class is being created and it has been shared by differentĀ threads.
14th Apr 2017, 4:57 AM
Dev
Dev - avatar
+ 5
It is a better programming practice to implement your code using Runnable and not Thread. Because you shouldn't decide right there how your task will be executed. An Executor should be used to execute your Runnable maybe on one thread or multiple threads or using thread pool, etc..
14th Apr 2017, 12:41 PM
TamƔs Barta
TamƔs Barta - avatar
+ 4
i use ExectorService cause idk how to use Thread or implement runnable lol
14th Apr 2017, 4:59 AM
Edward