What does start() function do in multithreading in Java
The thread begins by using the thread class's start() function. Two threads are created as a result of this function: the run thread (which carries out its run method) and the current thread (which returns from the call to the start method). To execute the code given in the run() function in a different thread, the start() method internally invokes the Runnable interface's run() method. The start() function is used to establish a distinct call stack for the thread. It creates a distinct call stack, and JVM calls run() after that.
The following are the tasks carried out by the start thread:
- The current thread-is new.
- The thread transitions to a runnable state from a new one.
- The thread's target run() method will be called when it has the chance to execute.
The following steps occur when a function is called:
Step 1: The given-arguments are evaluated.
Step 2: The call stack receives a new stack frame.
Step 3: First, the parameters are initially set up.
Step 4: The body of the method is used.
Step 5: The current stack frame is removed from the call stack and the value is returned.
Syntax:
public void start()
It gives no value in return.- But if the start() method is invoked more than once, an IllegalThreadStateException exception is raised.
Implementation:
FileName: MultithreadingEx.java
import java.io.*;
import java.util.*;
class ThreadStart extends Thread
{
public void run()
{
try
{
// Displaying that a particular thread is running
System.out.println ("Thread " + Thread.currentThread().getId() + " is running currently…");
}
catch (Exception e)
{
// Exception is throwing
System.out.println ("Exception is thrown");
}
}
}
public class MultithreadingEx
{
public static void main(String[] args)
{
int p = 4;
// let "p" indicates that four processes are running
for (int i=0; i<p; i++)
{
ThreadStart ob = new ThreadStart();
// run() is used in place of start() for understanding
//the purpose of the start
ob.run();
}
}
}
Output:
Thread 1 is running currently…
Thread 1 is running currently…
Thread 1 is running currently…
Thread 1 is running currently…
Significance of start() method:
To create a new call stack for the thread, the start() method must be called.
Begins to execute in parallel: The code inside the run() method is executed by the Java Virtual Machine (JVM) after receiving a signal from the start() function to allocate a new thread. As a result, the thread can run concurrently and independently of the main thread.
Preserves multithreading: The run() method will execute in the same thread if it is called directly without first using start(), which goes against the intent of multithreading. The run() method operates in a separate thread due to the start() method's guarantee.
Controls the thread lifecycle: In addition to starting the thread, the start() function places it in a "Runnable state." It enables the program's thread scheduler to control how this thread is executed in relation to other threads.