The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread.
In Core Java, the method that is invoked to cause an object to begin executing as a separate thread is the start()
method. The start()
method is defined in the Thread
class, and when called on an instance of a class that extends Thread
or implements the Runnable
interface, it initiates the execution of the thread.
Here’s a brief example using the Thread
class:
class MyThread extends Thread {
public void run() {
// Code to be executed in the separate thread
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start(); // This invokes the start() method and starts the separate thread
}
}
Alternatively, you can use the Runnable
interface and create a Thread
object with a Runnable
target:
class MyRunnable implements Runnable {
public void run() {
// Code to be executed in the separate thread
}
}
public class Main {
public static void main(String[] args) {
Thread myThread = new Thread(new MyRunnable());
myThread.start(); // This invokes the start() method and starts the separate thread
}
}
In both cases, calling the start()
method is essential to initiate the execution of the thread.