Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Programs For Performing a Single Task by Multiple Threads
3.
Programs for Performing Multiple Tasks Using Multiple Threads
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Perform single task by multiple threads

Author Nishant Rana
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In java, threads refer to the path that a code follows during its execution. The Java Virtual Machine (J.V.M.) creates the main thread at the start of the program when the main() method of the program is invoked.

We can perform a single task using the multiple threads by writing a single run() method for all the threads.

Programs For Performing a Single Task by Multiple Threads

Code 1:-

 

class Main extends Thread{  
    public void run(){  
        System.out.println("Single Task");  
    }  
    public static void main(String args[]){
         Main thread1=new Main();  
         Main thread2=new Main();  
         Main thread3=new Main();  
          
         thread1.start();  
         thread2.start();  
         thread3.start();
    }  
}  

 

Output: 

Practice by yourself on java online compiler.
 

Code 2:-
 

class Main extends Thread{  
    public void run(){  
        System.out.println("Single Task");  
    }  
    public static void main(String args[]){

         //Making a thread using the Thread class and anonymous object.
         Thread thread1=new Thread(new Main());  // passing an anonymous object
         Thread thread2=new Thread(new Main());  
         Thread thread3=new Thread(new Main());  
          
         thread1.start();  
         thread2.start();  
         thread3.start();
    }  
}  
 

Output:

We need to notice above that all the threads are running in the different call stacks, which can be seen in the following picture.
 


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

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

Programs for Performing Multiple Tasks Using Multiple Threads

We can implement multiple run() methods to perform multiple tasks using multiple threads.

Code 1:

class Thread1 extends Thread{  
    public void run(){  
        System.out.println("First Task");  
    }  
}  

class Thread2 extends Thread{  
    public void run(){  
        System.out.println("Second Task");  
    }  
}  

class Main{  
    public static void main(String args[]){  
        Thread1 t1=new Thread1();  
        Thread2 t2=new Thread2();  
        
        t1.start();  
        t2.start();  
    }  
}  

 

Output:

Code 2:

The same code as above but using the anonymous objects that implements the runnable interface.

 

class Main{  
    public static void main(String args[]){  
    
        Runnable obj1=new Runnable(){  
            public void run(){  
              System.out.println("First Task");  
            }  
        };  
        
        Runnable obj2=new Runnable(){  
            public void run(){  
              System.out.println("Second Task");  
            }  
        };  
          
        Thread thread1=new Thread(obj1);  
        Thread thread2=new Thread(obj2);  
        
        thread1.start();  
        thread2.start();  
    }  
}  

Output:


 

Must Read: Multithreading in C# , Multithreading in Python  and, Multithreading Operating System.

FAQs

  1. What are threads?
    In java, threads refer to the path that a code follows during its execution. The Java Virtual Machine (J.V.M.) creates the main thread at the start of the program when the main() method of the program is invoked.
  2. What happens when we call the start() method?
    As soon as, we call the start() method, the run() method of the thread is invoked.

Key Takeaways

In this blog, we have covered the following things:

  1. We first discussed what are Threads.
  2. Then we discussed how to perform single and multiple tasks using multiple threads.

If you want to learn more about Programming Language and want to practice some questions which require you to take your basic knowledge on Programming Languages a notch higher, then you can visit our here

Until then, all the best for your future endeavors, and Keep Coding.



 

Previous article
Difference between run() and start() method
Next article
Introduction to Java Networking
Live masterclass