What is multithreading?

Multithreading is a type of multitasking. Java supports built-in multithreaded programming. In multithreading, a program has two or more parts that run concurrently. Each part is a thread, and every single thread describes a unique execution path. Multiple threads execute parallel to each other.

Further, multiple threads are not allocated separate memory areas. Consequently, they consume less memory.

Java Thread

A Java thread is a lightweight process. Java uses the Thread Class to create threads. Java threads are of two types - user thread and daemon thread. A user thread is created when an application starts. Later, several user threads and daemon threads can be created.

Lifecycle of Java Thread

A thread goes through five stages during its lifecycle. Java virtual machine (JVM) controls the thread lifecycle. Below are the stages of the thread lifecycle:

Representation of lifecycle of a Java Thread

Methods used for implementing Multithreading in Java

In Java, the thread class uses several methods to manage threads.

Method NameDescription
getNameTo obtain the thread name
getPriorityTo find the thread priority
isAliveTo identify if the thread is still running
joinTo wait for a thread to terminate
runTo act as a thread
sleepTo stop the thread for some time
startTo start the thread by invoking the run method
isDaemonTo check if the thread is a daemon thread
setDaemonTo mark the thread as a daemon or user thread
activeCountTo find the number of active threads in the current thread’s group

Creating a thread in Java

In Java, the methods and constructors for creating a new thread are available in the thread class. Some of the widely-used constructors are:

  1. Thread () 
  2. Thread (String name) 
  3. Thread (Runnable r) 
  4. Thread (Runnable r, String name)

Furthermore, there are two approaches to implementing a thread:

  1. By extending the thread class
  2. By implementing the Runnable interface

Creating thread by extending the thread class

To create a thread using this method, a new class that extends the java Thread class is created. This class overrides the run method to create an instance of the class. The thread executes the run method after invoking the start () method.

Here is an example of creating a thread by extending the thread class.

class Multi extends Thread {  

public void run() {  

System.out.println ("thread1 is executing...");  

}  

public static void main (String args[]){  

Multi t2 = new Multi ();  

t2.start ();  

 }  

}  

Output: By running the above Java code, the output will be: 

thread1 is executing...

Creating thread by implementing the Runnable interface

To create a thread using the Runnable interface, a class that implements the Runnable interface is created, and the run() method is overridden. Then the instances are instantiated using a Thread object, and the start() method is called on this object.

Below is an example of creating a thread by implementing the Runnable interface.

class Multi implements Runnable {  

public void run() {  

System.out.println ("thread2 is executing...");  

}  

public static void main (String args[]) {  

Multi m1 = new Multi ();  

Thread t2 = new Thread (m1); // Using the constructor Thread(Runnable r)  

t2.start ();  

 }  

}  

Output: After running the above Java program, the output will be: 

thread2 is executing...

Thread priorities

In multithreading, the operating system needs to decide which thread to execute first. To solve this issue, the threads are assigned a priority. This makes it easier for the operating system to execute the thread.

The operating system has to determine the higher-priority threads and execute them first. Later, the threads with lower priority can be executed. The priorities can be between 1 to 10, where the MIN_PRIORITY is 1, NORM_PRIORITY (default) is 5, and MAX_PRIORITY is 10.

Advantages of multithreading

There are several reasons to use multithreading in Java, including:

  • Threads are independent. Hence, it does not block the user and supports the execution of multiple operations simultaneously.
  • Multithreading saves time and memory.
  • If an exception occurs in a single thread, it will not affect the execution of other threads.

Multithreading vs. Multiprocessing

Multithreading and multiprocessing are used to achieve multitasking in Java. However, both methods have several differences, as mentioned below.

MultiprocessingMultithreading
It is process-based multitaskingIt is thread-based multitasking.
Every process is allocated a separate memory space.Threads share the same memory space.
Processes are heavyweight.Threads are lightweight.
The cost of communication in this method is high.The cost of communication in this method is less.

Context and Applications

This topic is important in the professional exams for both undergraduate and graduate courses, including:

  • Masters of Software development
  • Bachelors in Computer Science engineering

Practice Problems

  1. How can a thread be created in Java multithreading?
  1. By extending Thread class.
  2. Implementing Runnable interface.
  3. Using multi-core dumpStack method.
  4. Both a and b.

Answer: Option d

Explanation: In multithreading, a thread can be created in two ways- by extending the Thread class and implementing the Runnable interface.

2. What is the maximum thread priority in Java multithreading?

  1. 10
  2. 5
  3. 6
  4. 8

Answer: Option a

Explanation: The maximum priority value that can be assigned to a thread in Java is 10.

3. Which method should be overridden while executing a thread?

  1. sub-processes()
  2. run()
  3. MyThread()
  4. start()

Answer: Option b

Explanation: A new class is created, and the public void run() method is overridden while executing a thread.

4. Which of the following are types of multitasking?

  1. Process-based.
  2. Thread-based.
  3. Child thread-based.
  4. Both a and b.

Answer: Option d

Explanation: Multitasking is of two types: Process-based multitasking (multiprocessing) and Thread-based multitasking (multithreading).

5. What is multithreading?

  1. It is a process in which two non-blocking processes run at the same time.
  2. It is a process in which two or more parts of the same process run simultaneously.
  3. It is a process in which two processes communicate simultaneously.
  4. None of the above.

Answer: Option b

Explanation: According to the definition, multithreading is a process in which two or more parts of the same process run simultaneously.

Common Mistakes

The terms multitasking, multiprocessing, and multithreading are often mixed up or confused by students. However, they are different terms.

  • Thread synchronization
  • Java inner classes
  • Basics of Java
  • Operators in Java

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Multithreading

Multithreading Methods

Multithreading methods Homework Questions from Fellow Students

Browse our recently answered Multithreading methods homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Multithreading

Multithreading Methods