Published by: sadikshya
Published date: 17 Jun 2021
Introduction to Multithreading is given below:-
Multithreading in java is a process of executing multiple threads simultaneously. Thread is basically a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than multiprocessing because threads share a common memory area. They don’t allocate separate memory areas so saves memory, and context-switching between the threads takes less time than process.
Multithreaded programming contains two or more parts that can run concurrently. Each piece of such a program is called a thread, and each thread defines a separate path of execution. Thus multithreading can be said as a particular version of multitasking. In this chapter, you will learn about how multithreading can be performed in Java and how they are useful to programmers.
Advantages of Java Multithreading
1) It doesn’t block the user because threads are independent and you can perform multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.
A thread goes through various stages of its life cycle. For example, first of all, a thread is born, started its tasks, run a sequence of tasks concurrently, and then dies. Here is the diagram of the various stages of a life cycle.
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
– Process-based Multitasking(Multiprocessing)
– Thread-based Multitasking(Multithreading)
Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements the Runnable interface.
The java.lang.Thread class is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Following are the important points about Thread −
Sometimes it is useful to suspend the execution of a thread. For example, suppose there is a thread that is used to display the time of day. If the user of that program does not want that clock, then the thread can be suspended. Once suspended, restarting that thread is also a simple matter, which is called resuming the thread. Here are the lists of pre-defined functions used in Java for performing suspending, resuming, and stopping threads respectively. They are:
The synchronized keyword is used to make the class or method thread-safe which means only one thread can have a lock of synchronized method and use it, other threads have to wait till the lock releases and anyone of them acquire that lock.It is important to use if our program is running in a multi-threaded environment where two or more threads execute simultaneously. But sometimes it also causes a problem which is called Deadlock. Below is a simple example of the Deadlock condition.