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();
} }
According to Java Doc
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.
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);
}
}
From this code, we can see that the start method is not calling the run()
method.