92
Java Thread start() method
The start() method of thread class is used to begin the execution of thread. The result of this method is two threads that are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
The start() method internally calls the run() method of Runnable interface to execute the code specified in the run() method in a separate thread.
The start thread performs the following tasks:
- It stats a new thread
- The thread moves from New State to Runnable state.
- When the thread gets a chance to execute, its target run() method will run.
Syntax
Return value
Exception
IllegalThreadStateException – This exception throws if the start() method is called more than one times.
Example 1: By Extending thread class
Output:
Thread is running...
Example 2: By Implementing Runnable Interface
Output:
Thread is running...
Example 3: When you call the start() method more than one time
Output:
First thread running... Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:708) at StartExp3.main(StartExp3.java:12)
Next TopicMultithreading in Java