Multithreading

Multithreading in Java:

Multithreading refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU. A thread in Java is a lightweight process requiring fewer resources to create and share the process resources.

Multithreading and Multiprocessing are used for multitasking in Java, but we prefer multithreading over multiprocessing. This is because the threads use a shared memory area which helps to save memory, and also, the content-switching between the threads is a bit faster than the process.

 The main reason for incorporating threads into an application is to improve its performance. Games and animations can also be made using threads.

Multiprocessing in Java:

Multiprocessing in Java is purely based on the number of processors available on the host computer. Every process initiated by the user is sent to the CPU (processor). It loads the registers on the CPU with the data related to the assigned process.

To perform multiprocessing in Java, the user needs one processor. Therefore, when the user requests the simultaneous execution of the second process, the alternate CPU core gets triggered and executes the process.

Advantages of Multithreading are:

  • Multithreading saves time as you can perform multiple operations together.
  • The threads are independent, so it does not block the user to perform multiple operations at the same time and also, if an exception occurs in a single thread, it does not affect other threads.

Life Cycle of a Thread:

There are five states a thread has to go through in its life cycle. This life cycle is controlled by JVM (Java Virtual Machine). These states are:

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated(Dead)

1. New:

In this state, a new thread begins its life cycle. This is also called a born thread. The thread is in the new state if you create an instance of Thread class but before the invocation of the start() method.

2. Runnable:

The next stage is the runnable stage. Here, a thread gets assigned to the task and sets itself for running the task. 

3. Running:

The third stage is the execution stage. Here, the thread gets triggered as control enters the thread, and the thread performs a task and continues the execution until it finishes the job. 

4. Non-Runnable (Blocked/Waiting):

At times, there is a possibility that one process as a whole might depend on another. During such an encounter, the thread might halt for an intermediate result because of its dependency on a different process. This stage is called the Waiting Stage.

5. Terminated(Dead):

The final stage of the process execution with Multithreading in Java is thread termination. After it terminates the process, the JVM automatically declares the thread dead and terminates the thread. This stage is known as the dead thread stage.

Methods of Multithreading in Java:

Following are the methods for Multithreading in Java.

start()The start method initiates the execution of a thread
currentThread()The currentThread method returns the reference to the currently executing thread object.
run()The run method triggers an action for the thread
isAlive() The isAlive method is invoked to verify if the thread is alive or dead
sleep()The sleep method is used to suspend the thread temporarily
yield()The yield method is used to send the currently executing threads to standby mode and runs different sets of threads on higher priority
suspend()The suspend method is used to instantly suspend the thread execution
resume()The resume method is used to resume the execution of a suspended thread only
interrupt()The interrupt method triggers an interruption to the currently executing thread class
destroy()The destroy method is invoked to destroy the execution of a group of threads
stop()The stop method is used to stop the execution of a thread

Multithreading vs. Multiprocessing in Java:

MultithreadingMultiprocessing
In this, multiple threads are created for increasing computational power using a single process.In this, CPUs are added in order to increase computational power.
Many threads of a process are executed simultaneously.Many processes are executed simultaneously.
It is not classified into any categories.Classified into two categories, symmetric and asymmetric.
The creation of a process is economical.Creation of a process is time-consuming.
In this, a common space of address is shared by all threadsEvery process in this owns a separate space of address.

Reference:

https://www.simplilearn.com/tutorials/java-tutorial/multithreading-in-java

https://www.geeksforgeeks.org/multithreading-in-java/

https://www.indeed.com/career-advice/career-development/multithreading-vs-multiprocessing#:~:text=What%20is%20multiprocessing%3F,to%20run%20multiple%20processes%20simultaneously.

https://www.indeed.com/career-advice/career-development/multithreading-vs-multiprocessing#:~:text=What%20is%20multiprocessing%3F,to%20run%20multiple%20processes%20simultaneously.

https://www.mygreatlearning.com/blog/multithreading-in-java/#:~:text=In%20Java%2C%20Multithreading%20refers%20to,and%20share%20the%20process%20resources.

https://www.scaler.com/topics/multithreading-in-java/

https://www.javatpoint.com/multithreading-in-java

Leave a comment

Design a site like this with WordPress.com
Get started