Java Thread start() methodThe 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:
SyntaxReturn valueExceptionIllegalThreadStateException - This exception throws if the start() method is called more than one times. Example 1: By Extending thread classTest it NowOutput: Thread is running... Example 2: By Implementing Runnable InterfaceTest it NowOutput: Thread is running... Example 3: When you call the start() method more than one timeTest it NowOutput: 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
|