开发者

java thread which is better way?

开发者 https://www.devze.com 2022-12-22 11:37 出处:网络
while creating a thread in jav开发者_开发百科a, there is two ways such as Extending threads and Implement runnable Interface.

while creating a thread in jav开发者_开发百科a, there is two ways such as Extending threads and Implement runnable Interface. I am unaware of Which is the better way of creating threads?


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.


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消