Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java threads
start
java programming
communitycreator

What is the Thread.start() function in Java?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The Thread.start() method from java.lang.Thread class is used to start thread execution. It implicitly calls the Thread.run() method to execute a newly created thread.

Syntax


public void start()

Parameters

NA: it does not take any argument.

Return value

Its return type is void, so it does not return anything.

IllegalThreadStateException: This exception will be raised when start() is called more than once.

Example 1

In the code snippet below, the start() method will execute the run() method from the Runnable interface and print some text:

// including Thread class
import java.lang.Thread;
// Main class
public class MainThread extends Thread implements Runnable {
public static void main(String args[]) {
MainThread thread = new MainThread();
// --- start() method ---
thread.start();
}
// overriding thread run method
@Override
public void run() {
System.out.println("I'm from run() method..");
System.out.println("Thread executing...");
}
}
Using the Thread.start() function in Java

Explanation

  • Line 2: We import the Thread class.
  • Line 6: We create a thread.
  • Line 8: We call the start() method.
  • Lines 11 to 15: We override the run() method.

Example 2

In the code snippet below, the start() method throws an IllegalThreadStateException exception when we call the start() method more than once:

// including Thread class
import java.lang.Thread;
// Main class
public class MainThread extends Thread implements Runnable {
public static void main(String args[]) {
MainThread thread = new MainThread();
// --- start() method ---
thread.start();
thread.start();
}
// overriding thread run method
@Override
public void run() {
System.out.println("I'm from run() method..");
System.out.println("Thread executing...");
}
}
Using the Thread.start() function in Java

Explanation

  • Line 2: We import the Thread class.
  • Line 6: We create a thread.
  • Lines 8 and 9: We call the start() method twice.
  • Lines 12 to 16: We override the run() method.

RELATED TAGS

java threads
start
java programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring