Home/Notes/Multithreading
Board Exam Notes

Multithreading Notes

Questions

1–2 conceptual questions per paper

Difficulty

Medium

Importance

High yield for University theory exams and Java technical interviews

Overview

Multithreading in Java is a concurrent execution mechanism that allows multiple parts of a program to run simultaneously, optimizing CPU utilization. Mastering this topic is essential for semester exams as it tests your understanding of Java's memory model and task scheduling. You must understand how threads move through various states and how shared resources are protected during concurrent access.

Thread Lifecycle

The lifecycle of a thread consists of specific states that define its progression from creation to termination. Understanding these transitions is critical for debugging thread behavior in exam scenarios.

  • New: Created but start() method not called
  • Runnable: Ready for execution by the thread scheduler
  • Running: Thread is currently executing its run() method
  • Blocked/Waiting: Thread is waiting for a monitor or resource
  • Terminated/Dead: Thread has finished execution

Runnable vs Thread Class

Java provides two primary ways to create a thread: extending the Thread class or implementing the Runnable interface. Choosing the right approach is a frequent point of discussion in viva examinations.

  • Thread class is a concrete class; implementing Runnable is better for code design
  • Extending Thread prevents the class from inheriting from another class
  • Implementing Runnable allows multiple inheritance of interfaces
  • Thread class approach is easier to write for simple tasks
  • Runnable interface is preferred for decoupling task logic from the thread object

Synchronization

Synchronization is the process of controlling the access of multiple threads to any shared resource. This prevents data inconsistency and race conditions in multithreaded environments.

  • Uses the 'synchronized' keyword to lock objects
  • Mutual Exclusion: Ensures only one thread executes a block at a time
  • Inter-thread communication via wait(), notify(), and notifyAll()
  • Helps prevent 'Race Conditions' in memory access
  • Operates on object-level or class-level monitors

Exam Tip

Always mention that calling start() triggers the internal JVM mechanisms to spawn a new thread, whereas run() is just a normal method call within the current thread.

Common Mistakes

  • Confusing the start() method with the run() method; calling run() directly does not create a new thread
  • Failing to handle InterruptedException when using sleep() or wait()
  • Attempting to synchronize on null objects, causing runtime errors

More Revision Notes

Ready to test yourself?

Play topic-wise Multithreading questions in Aspirant Arcade — gamified MCQ practice.

Download Free