Besides the main thread, developers can instantiate their own when:
Thread classThread is passed an implemented Runnable instanceOverride the run() method in both cases to define the program to be run concurrently in the new thread, then call the Thread instance’s start() method.
Threadpublic class CustomThread extends Thread {
@Override
public void run() {
// Do something
}
public static void main(String[] args) {
new CustomThread().start();
}
}
Runnablepublic class CustomRunnable implements Runnable {
@Override
public void run() {
// Do something
}
public static void main(String[] args) {
new Thread(new CustomRunnable()).start();
}
}
Runnable Classnew Thread(new Runnable() {
public void run() {
// Do something
}
}).start();
Runnable Lambdanew Thread(
() -> { /* Do something */ };
).start();