java 当子类从线程父类扩展时,我们是否覆盖 run 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4161400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do we override run method when child class is extended from thread parent class
提问by ranjanarr
When we Subclass Thread, do we override its run method? We know Thread class itself implements Runnable, but there is no body for run method defined in Runnable class.
当我们对 Thread 进行子类化时,我们是否覆盖了它的 run 方法?我们知道Thread类本身实现了Runnable,但是Runnable类中并没有定义run方法体。
This is picture in my mind:
这是我脑海中的画面:
Runnable - Parent class-it has a run method in it, with empty body.
Runnable - 父类 - 它有一个 run 方法,主体为空。
Thread- Child,
线程 - 儿童,
classA extends Thread- Child of Child,
classA 扩展了 Thread-Child of Child,
when we define run() method in "classA" , are we overriding run method declared in Runnable class? Thank you for your time.
当我们在 "classA" 中定义 run() 方法时,我们是否覆盖了 Runnable 类中声明的 run 方法?感谢您的时间。
回答by shj
There are two ways to define the behavior of a thread: subclass the Thread class, or implement the Runnable interface.
有两种方法可以定义线程的行为:子类化 Thread 类,或实现 Runnable 接口。
For the first way, simply extend the Thread class and override the run() method with your own implementation:
对于第一种方法,只需扩展 Thread 类并使用您自己的实现覆盖 run() 方法:
public class HelloThread extends Thread {
@Override
public void run() {
System.out.println("Hello from a thread!");
}
}
public class Main {
// main method just starts the thread
public static void main(String args[]) {
(new HelloThread()).start();
}
}
However, the preferred way of implementing the logic for a Thread is by creating a class that implements the Runnable interface:
但是,实现 Thread 逻辑的首选方法是创建一个实现 Runnable 接口的类:
public class HelloRunnable implements Runnable {
@Override
public void run() {
System.out.println("Hello from a thread!");
}
}
public class Main {
// notice that we create a new Thread and pass it our custom Runnable
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
The reason that implementing Runnable is preferred is that it provides a clear separation between the behavior of the thread and the thread itself. For instance, when using a thread-pool you never actually create the thread from scratch, you just pass a Runnable to the framework and it'll execute it on an available thread for you:
首选实现 Runnable 的原因是它在线程的行为和线程本身之间提供了清晰的分离。例如,当使用线程池时,您实际上从未从头开始创建线程,您只需将 Runnable 传递给框架,它就会在可用线程上为您执行它:
public class Main {
public static void main(String args[]) {
int poolSize = 5;
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
pool.execute(new HelloRunnable());
}
}
Further Reading:
进一步阅读:
回答by Dead Programmer
You should extend the Thread , only when are you going to rewrite the functionality of thread or improve its performance .
只有当你打算重写线程的功能或提高其性能时,才应该扩展线程。
Interface tells you that , if you use this interface , you will get the funcationality.In your case , your buisness logic needs to be run in a thread then use interface.
接口告诉你,如果你使用这个接口,你将获得功能。在你的情况下,你的业务逻辑需要在一个线程中运行,然后使用接口。
If you a better way of running thread efficiently then extend the Thread.
如果您有更好的方式有效地运行线程,那么请扩展 Thread。