Multithreading in Java

Multithreading is a process of executing multiple threads simultaneously.
Thread is basically a lightweight subprocess, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than mulitprocessing because threads share a common memory area. They don't allocate separate memory area so save memory, and context-switching between the threads takes less time than processes.
Multithreading is mostly used in games, animation etc.


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 have its own address in memory i.e. each process allocates separate memory area.
  • Process is heavyweight.
  • Cost of communication between the process is high.
  • Switching from one process to another require 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.
  • Cost of communication between the thread is low.
  • Note:At least one process is required for each thread.

What is Thread?

A thread is a lightweight subprocess, a smallest unit of processing. It is a separate path of execution. It shares the memory area of process.
As shown in the above figure, thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.
Note:At a time only one thread is executed.

Do You Know ?
  • Why your class, which extends the Thread class, object is treated as thread ? Who is responsible for it ?
  • How to perform two tasks by two threads ?
  • How to perform multithreading by annonymous class ?
  • What is the Thread Schedular and what is the difference between preemptive scheduling and time slicing ?
  • What happens if we start a thread twice ?
  • What happens if we call the run() method instead of start() method ?
  • What is the purpose of join method ?
  • Why JVM terminates the daemon thread if there is no user threads remaining ?
  • What is the shutdown hook?
  • What is garbage collection ?
  • What is the purpose of finalize() method ?
  • What does gc() method ?
  • What is synchronization and why use synchronization ?
  • What is the difference between synchronized method and synchronized block ?
  • What are the two ways to perform static synchronization ?
  • What is deadlock and when it can occur ?
  • What is interthread-communication or cooperation ?
What we will learn in Multithreading ?
  • Multithreading
  • Life Cycle of a Thread
  • Two ways to create a Thread
  • How to perform multiple tasks by multiple threads
  • Thread Schedular
  • Sleeping a thread
  • Can we start a thread twice ?
  • What happens if we call the run() method instead of start() method ?
  • Joining a thread
  • Naming a thread
  • Priority of a thread
  • Daemon Thread
  • ShutdownHook
  • Garbage collection
  • Synchronization with synchronized method
  • Synchronized block
  • Static synchronization
  • Deadlock
  • Inter-thread communication