Introduction to Multithreading

Filter Course


Introduction to Multithreading

Published by: sadikshya

Published date: 17 Jun 2021

Introduction to Multithreading photo

Introduction to Multithreading

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.

Life Cycle of 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.

  1. New Thread: A new thread begins its life cycle in the new state. The process remains in this condition until the program starts the thread.
  2. Runnable: As soon as the new thread starts, the thread status becomes Runnable. At this stage, a thread is considered to execute its function or work.
  3. Not Runnable: A Runnable thread when entered the time of waiting for the state for a specific interval of time. That time, the thread is not in Runnable condition.
  4. Dead / Terminated: The Runnable thread enters the end-stage when it completes its tasks.

Multitasking

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)

  1. Process-based Multitasking (Multiprocessing)
    Each process has its own address in memory i.e. each process allocates separate memory area.
    The process is heavyweight.
    The cost of communication between the process is high.
    Switching from one process to another requires some time for saving and loading registers, memory maps, updating lists etc.
  2. Thread-based Multitasking(Multithreading)
    Threads share the same address space.
    Thread is lightweight.
    The cost of communication between the thread is low.

 Thread class

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 −

  • Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority
  • Each thread may or may not also be marked as a daemon.
  • There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread and,
    the other way to create a thread is to declare a class that implements the Runnable interface

Suspending  Resuming and Stop Threads

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:

  1. suspend()
  2. resume()
  3. stop()

Deadlock

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.