简体   繁体   English

java线程哪个更好的方法?

[英]java thread which is better way?

while creating a thread in java, there is two ways such as Extending threads and Implement runnable Interface. 在Java中创建线程时,有两种方法,例如扩展线程和实现可运行接口。 I am unaware of Which is the better way of creating threads? 我不知道哪个是创建线程的更好方法?

显然,实现Runnable更好,因为它可能允许您使用无法与Thread一起使用的线程池和执行队列,除了显而易见的事实是,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. 实施Runnable更好。 Josh Bloch covered it in correspondent chapter of Effective Java. Josh Bloch在“有效Java”的相应章节中介绍了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM