Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Thread start() method
2.1.
Syntax
3.
Example
3.1.
Code 1
3.2.
Code 2
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Start() method in thread

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In this blog, we shall be covering the start() method in the Thread. However, before moving to the main topic, we shall brush up few things like what a thread is. These are simply programs that help operate more efficiently by performing multiple tasks simultaneously. We shall be learning about the start method and its implementation in a thread.

Java Thread start() method

The start() method of the Thread class helps us to start the Thread's execution. The result of this method is two threads that are running concurrently. The current thread returns from the call to begin the start method, and the other executes the Thread.

This start() method will internally call the run() method of Runnable interface to execute the code which is specified in the run() method in a separate thread.

Syntax

public void start()
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

Code 1

public class ThreadStart {
public void run()
{
     try
     {
          
         System.out.println ("Thread " +
         Thread.currentThread().getId() +
         " is running");
      
     }
     catch (Exception e)
     {
          
         System.out.println ("Exception is caught");
     }
     }
    
    
    public static void main(String[] args)
    {
     int n = 5;
     for (int i=0; i<n; i++)
     {
         ThreadStart object = new ThreadStart();
          
         object.run();
     }
}
}

Output

Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running


In this example, we have used the start() method to begin the Thread. We have printed the number of times that a thread is working.

Try it on online java compiler.

Code 2

public class StartThreads extends Thread  
{    
    public void run()  
    {    
        System.out.println("The Thread is running...");    
    }    
    public static void main(String args[])  
    {    
     StartThreads t1=new StartThreads();    
        
        t1.run();    
    }    
}    

Output

The Thread is running…


In this example, we have started the Thread using the start() method and printed the output.

Also see, Duck Number in Java and Hashcode Method in Java

FAQs

  1. How to start a thread in Java?
    We can start a thread in Java by one of the two processes: Extending Thread class and implementing runnable In both cases, we override the run() functions.
     
  2. What is the use of the start method in Java?
    The Java Thread start () method is used to begin executing a thread. 
     
  3. How to start and run a thread in Java?
    A thread starts its life cycle inside the run() method after creating an object of our new class and calling the start() method to start the execution of a thread.

Key Takeaways

In this article, we have covered the topic of the start() method in the Thread. We have given a brief introduction to the start() method. We also explained a Java Thread start() method and its syntax. We also took a few examples of the start() method and output. If you want to learn more about Java, please visit here Introduction To Java. We hope that this article has helped you enhance your knowledge regarding the start() method in the Thread. If you like my article, please upvote it to help ninjas grow. "Happy Coding!".

Previous article
Thread scheduler
Next article
Types of Threads in Java
Live masterclass