Multithreading in Java

Multithreading in java is a process of executing multiple threads simultaneously.

Thread is basically a lightweight sub-process, a 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 area so saves

memory, and context-switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation etc.

Advantages of Java Multithreading

  1. It doesn't block the user because threads are independent and you can perform multiple operations at 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 exception occur in a single thread.


Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:

  1. Process-based Multitasking(Multiprocessing)
  2. Thread-based Multitasking(Multithreading)

1) Process-based Multitasking (Multiprocessing)

  1. Each process have its own address in memory i.e. each process allocates separate memory area.
  2. Process is heavyweight.
  3. Cost of communication between the process is high.
  4. Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.

2) Thread-based Multitasking (Multithreading)

  1. Threads share the same address space.
  2. Thread is lightweight.
  3. Cost of communication between the thread is low.

What is Thread in java

A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.

Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

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 one thread is executed only.

Do You Know

  1. How to perform two tasks by two threads ?
  2. How to perform multithreading by annonymous class ?
  3. What is the Thread Schedular and what is the difference between preemptive scheduling and time slicing ?
  4. What happens if we start a thread twice ?
  5. What happens if we call the run() method instead of start() method ?
  6. What is the purpose of join method ?
  7. Why JVM terminates the daemon thread if there is no user threads remaining ?
  8. What is the shutdown hook?
  9. What is garbage collection ?
  10. What is the purpose of finalize() method ?
  11. What does gc() method ?
  12. What is synchronization and why use synchronization ?
  13. What is the difference between synchronized method and synchronized block ?
  14. What are the two ways to perform static synchronization ?
  15. What is deadlock and when it can occur ?
  16. What is interthread-communication or cooperation ?

What we will learn in Multithreading

  1. Multithreading
  2. Life Cycle of a Thread
  3. Two ways to create a Thread
  4. How to perform multiple tasks by multiple threads
  5. Thread Schedular
  6. Sleeping a thread
  7. Can we start a thread twice ?
  8. What happens if we call the run() method instead of start() method ?
  9. Joining a thread
  10. Naming a thread
  11. Priority of a thread
  12. Daemon Thread
  13. ShutdownHook
  14. Garbage collection
  15. Synchronization with synchronized method
  16. Synchronized block
  17. Static synchronization
  18. Deadlock
  19. Inter-thread communication

Copyright 2017 Design& Development