Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Multithreading in Java – Learn with Examples

Updated on 30 November, 2022

5.24K+ views
6 min read

Java is a programming language that supports multithreaded programs. Multithreaded programs accommodate two or more parts (threads) that run simultaneously, handling different tasks, enabling optimal CPU usage. Multi-threading is an extension of multi tasking. Multi tasking can be defined as a feature where multiple processes share a common processing resource like a central processing unit. 

Multithreading branches out the concept of multi-tasking to various applications where defined operations can be broken down into smaller individual threads.

Each of these threads runs simultaneously and independently with different execution paths such that an exception in one thread does not affect the performance of others. 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

What is Multithreading in Java?

Multithreading in Java is where multiple threads are executed simultaneously to maximize the CPU time.

Life Cycle of a Thread

Threads have different stages in their life cycle. They are as follows:

  • New: The thread begins its life cycle as a new thread. It remains in this newborn stage until a program runs the thread. 
  • Runnable: A born thread enters the runnable stage when a program initiates it. The thread starts executing its task in this stage.
  • Waiting: This is a transition stage where a thread waits for another thread to complete its task. A thread in the waiting stage switches to a runnable stage after receiving a signal from other threads.
  • Timed waiting: This is a stage that a runnable thread can enter for a specific time interval. Threads in this stage move back to the runnable stage at the end of the specific time interval. 
  • Dead: This is the final stage in the life cycle of a thread. A thread enters this stage after completing its task. 

Benefits of Multithreading in Java

  • As threads in Java are independent, Java multithreading does not block the users, and they can perform many operations simultaneously. 
  • As multithreading in Java enables multiple operations, it is time-saving and efficient. 
  • The program can run continuously even if a part of it is intercepted.
  • It improves the performance against the conventional parallel programs that use multiple processes.
  • Creation of efficient programs that utilize the CPU time to its fullest.
  • Greatly improves the response time of complex applications. 
  • Requires fewer resources compared to traditional parallel programs.

Order of Thread Priority

The operating system determines the thread’s execution schedule based on the thread’s priority. A thread’s priority is determined based on its constant’s value. 

  • Constant 1 gets minimum priority ( MIN_PRIORITY)
  • Constant 5 gets normal priority ( NORM_PRIORITY)
  • Constant 10 gets the maximum priority (MAX_PRIORITY)

How to create a thread in Java?

Threads can be created in Java in two ways:

  1. Extending thread class
  2. Implementing a runnable interface

Creating a Thread by Extending Thread Class

A few methods of thread class and their functions are given below. 

  • getName() : For obtaining thread’s name
  • getPriority(): To obtain thread’s priority
  • is Alive(): to find if a thread is running 
  • join(): wait for a thread’s termination
  • run() : Point of thread’s entry
  • sleep(): For suspending a thread for a specific time window.
  • start() : To activate a thread through its run() method.

Step 1: Override the run() method given in the thread class. This acts as the entry point for the thread, and the entire program logic should be encased in 4this. 

The syntax of the run() method is as follows:

public void run ( )

Step 2:  Initiate the thread object by start () method whose syntax is a void start ( )

Here are examples of a multithreading program in Java:

Example 1:

class MultithreadingDemo extends Thread{  

  public void run(){  

    System.out.println(“My thread is running.”);  

  }   

  public static void main(String args[]){  

     MultithreadingDemo obj=new MultithreadingDemo();   

     obj.start();  

  }  

}

The output of the above program will be:

My thread is running.

Example 2: 

class Count extends Thread

{

   Count()

   {

     super(“my thread is extending”);

     System.out.println(“my thread is created” + this);

     start();

   }

   public void run()

   {

     try

     {

        for (int i=0 ;i<10;i++)

        {

           System.out.println(“Printing the count ” + i);

           Thread.sleep(1000);

        }

     }

     catch(InterruptedException e)

     {

        System.out.println(“my thread got interrupted”);

     }

     System.out.println(“My thread is over” );

   }

}

class ExtendingExample

{

   public static void main(String args[])

   {

      Count cnt = new Count();

      try

      {

         while(cnt.isAlive())

         {

           System.out.println(“Main thread will be alive till the child thread lives”);

           Thread.sleep(1500);

         }

      }

      catch(InterruptedException e)

      {

        System.out.println(“Main thread got interrupted”);

      }

      System.out.println(“Main thread’s run is terminated” );

   }

}

The output of the above program will be 

my thread is createdThread[my runnable thread,5,main]

Main thread will be alive till the child thread lives

Printing the count 0

Printing the count 1

Main thread will be alive till the child thread lives

Printing the count 2

Main thread will be alive till the child thread lives

Printing the count 3

Printing the count 4

Main thread will be alive till the child thread lives

Printing the count 5

Main thread will be alive till the child thread lives

Printing the count 6

Printing the count 7

Main thread will be alive till the child thread lives

Printing the count 8

Main thread will be alive till the child thread lives

Printing the count 9

my thread run is terminated

Main thread run is terminated

Creating a Thread by Implementing a Runnable Interface

There are three important steps to be followed while using this method. 

Step 1: A run() method should be implemented by a runnable interface. This run() method acts as the entry point for the thread and should contain the entire program logic. 

Syntax for the run() method is 

public void run( )

Step 2:  A thread object should be activated using the constructor given below.

Thread(Runnable threadObj, String threadName);

Here threadObj acts as the class that implements the runnable interface, and thread name is the name given to the new thread.

Step 3: After creating the thread object, it can be initiated by the start() method that executes the run() method. 

The syntax of the start () method is as follows.

void start();

An example of creating a thread by implementing a runnable interface can be found here.

To learn more about Java multithreading and software development, we recommend enrolling in the Executive Post Graduate Program in Software Development offered by IIIT Bangalore in association with upGrad.

Executive PG Program in Software Development is a 13-month online program designed to help working professionals take a giant leap in their careers through industry-relevant case studies and projects. The program also includes over ten live sessions from the industry experts to help the candidates learn better. 

The program includes industry projects to help the candidates have hands-on experience and adequate industry exposure to make their learning relevant and practical. The completion of the program will reward the candidates with the coveted IIIT B certification and alumni status. 

The program comes with 360-degree placement support from upGrad that has impacted over 40,000 paid learners from all corners of the world to make things even better. upGrad has a learners base from over 85 countries providing a priceless opportunity to have a global peer network for the candidates. 

Frequently Asked Questions (FAQs)

1. What is the difference between multitasking and multithreading in Java?

Multitasking is the process by which many tasks can be performed simultaneously, and multithreading is the process of executing multiple threads at the same time, with each thread performing a different task.

2. Is multiple inheritance supported by Java?

The programming language Java supports multiple inheritance, a process in which a class implements more than one interface. A class can have different implementations of a method (default or static) in the interfaces. Multiple inheritance in Java can be implemented only with interfaces and not classes, unlike the C++ language.

3. Can multithreading improve the performance of Java?

Multithreading improves the performance of Java by enabling multiple CPUs to attend to the same issue. This not only speeds up the process but also helps in efficient problem-solving.

RELATED PROGRAMS