![]() |
VOOZH | about |
In Java, threads enable concurrent execution of multiple tasks within a program. They help improve performance and responsiveness in applications.
The purpose of start() is to create a separate call stack for the thread. A separate call stack is created by it, and then run() is called by JVM. The main purpose of the start() method is to create a separate call stack for a new thread. When start() is invoked:
Because of this, multiple threads can execute simultaneously.
Example: Java code to see that all threads are pushed on same stack if we use run() instead of start().
Output:
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Explanation: Calling run() directly executes the method in the main thread, so no new thread is created and the same thread ID is printed every time. Since there is no multithreading, all 8 executions run sequentially in a single call stack.