Java Thread start() Method
Example
Create a thread and start it:
public class Main {
static class MyThread extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
Definition and Usage
The start() method starts a thread. The thread then runs the code in its run() method, at the same time as the rest of the program.
You can start a thread only one time. To run the code again, create a new thread object.
Tip: Use the join() method to wait for the thread to finish.
Syntax
public void start()
Parameter Values
None.
Technical Details
| Throws: | IllegalThreadStateException - if the thread was already started. |
|---|
Related Pages
❮ Thread Methods