What is the Difference Between Single Thread and Multi Thread in Java

The main difference between single thread and multi thread in Java is that single thread executes tasks of a process while in multi-thread, multiple threads execute the tasks of a process.

A process is a program in execution. Process creation is a resource consuming task. Therefore, it is possible to divide a process into multiple units called threads. A thread is a lightweight process. It is possible to divide a single process into multiple threads and assign tasks to them. When there is one thread in a process, it is called a single threaded application. When there are multiple threads in a process, it is called a multi-threaded application.

Key Areas Covered

1. What is Single Thread in Java
  – Definition, Functionality
2. What is Multi Thread in Java
  – Definition, Functionality
3. Difference Between Single Thread and Multi Thread in Java
  – Comparison of key differences

Key Terms

Java, Single Thread, Multi Thread, Process

Difference Between Single Thread and Multi Thread in Java - Comparison Summary

What is Single Thread in Java

In a single thread or threading, the process contains only one thread. That thread executes all the tasks related to the process. An example program is as follows.

Difference Between Single Thread and Multi Thread in Java

Figure 1: Single Thread program in Java

In the above program, a and b are variables of type integer. There are is an object of type HelloWord. Using that object, the programmer can call Add, Sub, Mul and Div methods. These methods perform addition, subtraction, multiplication, and division. Finally, these methods display the answers on the console. There is only one thread and it displays the outputs.

What is Multi Thread in Java

In a multi-threaded application, multiple threads are executed concurrently. Each thread handles different tasks simultaneously by making optimal use of the resources. In Java, there are two methods to create threads. These are by implementing a Runnable interface or extending the Thread class. An example multi-threaded program is as follows.

Main Difference - Single Thread in Java vs Multi Thread in Java

Figure 2: Multi thread program in Java

Difference Between Single Thread and Multi Thread in Java_Figure 3

Figure 3: Class with main method

HelloWorld is the class that implements the Runnable interface. The run method has implementation. Runnable interface has this method. It is the starting point of the thread. Therefore, the business logic is inside this method. The threadName is the name of the new thread.  After creating the thread object, the programmer can start it using the start() method. It executes the run method. In the console, we can see that there are two threads executed in parallel.

Difference Between Single Thread and Multi Thread in Java

Definition

Single thread refers to executing an entire process from beginning to end without interruption by a  thread while multi-thread refers to allowing multiple threads within a process so that they are executed independently while sharing their resources. Thus, this is the main difference between single thread and multi thread in Java.

Basis

A single thread executes a process in single threading. Multiple threads execute a process in multi-threading. Hence, this is another difference between single thread and multi thread in Java.

Conclusion

Java is a popular programming language. One major advantage of using Java to develop applications is that it supports multi-threading. Therefore, it is possible to divide a single process into multiple threads so that each thread can execute tasks at the same time. The main difference between single thread and multi thread in Java is that single thread executes tasks of a process while in multi-thread, multiple threads execute the tasks of a process.

Reference:

1.“What Is Multithreading? – Definition from Techopedia.” Techopedia.com, Available here.
2.“Join Method in Java – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply