After a thread is started, via its start() method or that of the Thread class, the JVM invokes the thread’srun() method when the thread is initially executed.
In Java, a thread’s run()
method is invoked when the thread is started using the start()
method. The start()
method is part of the Thread
class and is used to begin the execution of the thread. When start()
is called, it in turn calls the run()
method of the thread.
Here’s a simple example:
public class MyThread extends Thread {
public void run() {
// Code to be executed when the thread is started
System.out.println("Thread is running");
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start(); // This will invoke the run() method
}
}
In this example, when myThread.start()
is called, it triggers the execution of the run()
method in a separate thread of control.