Object-Oriented Programming in Java

Threads


Module VI: Threads. This module explains Java’s multithreading model, how threads work, how to create and manage threads, and how to safely share resources.


The Java Thread Model

Multithreading allows multiple parts of a program to run concurrently. Each part runs in a separate thread. Java provides built-in support for threading at the language level.

  • Thread – a lightweight process
  • Multithreading – executing multiple threads
  • Context switching – CPU switching between threads

Diagram: thread-life-cycle.png


The Main Thread

Every Java program starts with one thread: the main thread.

public class MainThreadDemo {
  public static void main(String[] args) {
    Thread t = Thread.currentThread();
    System.out.println(t.getName());      // main
    System.out.println(t.getPriority());  // default = 5
  }
}

Creating Threads

Java supports two ways to create threads:

  • Extending the Thread class
  • Implementing the Runnable interface
Method 1: Extending Thread
class MyThread extends Thread {
  public void run() {
    System.out.println("Thread running");
  }
}

MyThread t = new MyThread();
t.start();
Method 2: Implementing Runnable
class MyTask implements Runnable {
  public void run() {
    System.out.println("Runnable running");
  }
}

Thread t = new Thread(new MyTask());
t.start();

Runnable is preferred when a class must extend another class.


Creating Multiple Threads

for (int i = 1; i <= 3; i++) {
  Thread t = new Thread(() -> {
    System.out.println("Running: " + Thread.currentThread().getName());
  });
  t.start();
}

Each thread runs independently. Order of execution is not guaranteed.


Thread Priorities

Every thread has a priority (1 to 10). Higher priority increases the chance of being scheduled first.

Thread t = new Thread(new MyTask());
t.setPriority(Thread.MAX_PRIORITY);  // 10

Synchronization

When multiple threads access shared data, race conditions may occur. Synchronization ensures only one thread accesses critical code at a time.

class Counter {
  private int count = 0;

  synchronized void increment() { count++; }
}

Only one thread can execute a synchronized method or block on an object at once.

Diagram: synchronization-lock.png


Interthread Communication

Java provides wait(), notify(), and notifyAll()for threads to communicate inside synchronized blocks.

synchronized(obj) {
  obj.wait();       // thread waits
  obj.notify();     // wakes one thread
}

Used in producer-consumer problems and buffering.


Using Multithreading

  • Parallel computation
  • Animations and background tasks
  • Real-time systems
  • Servers handling multiple clients

Summary

Threads enable concurrent execution in Java. Learning how to create threads, synchronize them, and manage communication is essential for advanced Java programming and real-world applications such as GUI programming, networking, and server-side processing.

Add diagrams: thread-cycle.png, synchronization-flow.png