Java Thread run() Method
Example
See the difference between calling run() and calling start():
public class Main {
static class MyThread extends Thread {
public void run() {
System.out.println("Running in: " + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.setName("Thread 1");
thread.run();
thread.start();
}
}
Definition and Usage
The run() method contains the code that a thread runs. You override it (write your own version of it) in a class that extends Thread, or in a class that implements the Runnable interface.
Do not call run() yourself to start a thread. Then the code runs in the current thread, like a normal method. Call start() instead, which runs run() in a new thread.
Tip: Use the start() method to run the code in a new thread.
Syntax
public void run()
Parameter Values
None.
Related Pages
❮ Thread Methods