哪个方法调用run()? [英] Which method calls run()?
本文介绍了哪个方法调用run()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
} }
根据 Java文档
Runnable
接口定义了一个单一方法run
,旨在包含在线程中执行的代码.将Runnable对象传递给Thread构造函数.
The
Runnable
interface defines a single method,run
, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor.
那么,当我们执行HelloRunnable时,谁调用了内部运行方法?在 Thread
类中, start
方法如下所示:
So, When we execute HelloRunnable, who calls the inside run method?
In the Thread
class, the start
method looks like this:
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}
从这段代码中,我们可以看到start方法没有调用 run()
方法.
From this code, we can see that the start method is not calling the run()
method.
推荐答案
查看全文