Skip to content

Topic 08: Multithreaded Programming

Zhamri Che Ani edited this page May 6, 2026 · 1 revision

Goal

To introduce students to multithreading concepts in Java, including thread creation, thread lifecycle management, synchronization, and thread coordination for developing concurrent and efficient applications.

🎯 Learning Objectives

Students should be able to:

  1. Explain the concepts of processes, threads, and concurrency.
  2. Differentiate between single-threaded and multithreaded applications.
  3. Define and create threads using:
    • Thread class
    • Runnable interface
    • Lambda expressions
  4. Start and manage multiple threads in a Java application.
  5. Describe the thread lifecycle and thread states.
  6. Apply methods that influence thread execution such as:
    • sleep()
    • yield()
    • join()
  7. Explain race conditions and critical sections in concurrent programming.
  8. Implement thread synchronization using:
    • synchronized methods
    • synchronized blocks
  9. Coordinate communication between threads using:
    • wait()
    • notify()
    • notifyAll()
  10. Develop simple multithreaded Java applications involving shared resources.

📌 Topics to Cover

  1. Introduction to Processes and Threads
    • Process vs thread
    • Concurrency and multitasking
    • Context switching
    • Lightweight processes
  2. Single-thread vs Multi-thread Applications
    • Advantages of multithreading
    • Resource sharing
    • Improved responsiveness
  3. Java Threads
    • JVM main thread
    • Thread call stack
    • Thread execution flow
  4. Defining Threads Extending the Thread class
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }
}

Implementing the Runnable interface

class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable thread");
    }
}

Lambda Expression with Runnable

Runnable r = () -> System.out.println("Lambda Thread");
  1. Creating and Starting Threads
    • Thread constructors
    • start() vs run()
    • Multiple thread execution
  2. Thread Lifecycle and States
    • NEW
    • RUNNABLE
    • BLOCKED
    • WAITING
    • TIMED_WAITING
    • TERMINATED
  3. Influencing Thread Execution
    • sleep(milliseconds)
    • yield()
    • join()
    • Thread scheduling
  4. Race Conditions and Critical Sections
    • Shared resources
    • Data inconsistency problems
    • Mutual exclusion
  5. Thread Synchronization Synchronized Methods
public synchronized void withdraw(int amt) {
    // critical section
}

Synchronized Blocks

synchronized(this) {
    // critical section
}
  1. Thread Communication
    • wait()
    • notify()
    • notifyAll()
    • Monitor locks
  2. Exception Handling in Threads
    • InterruptedException
    • Illegal monitor state issues

🛠️ Hands-on Activity

Activity 1: Creating a Simple Thread

Students create a Java program that:

  • Extends the Thread class
  • Displays thread messages using the run() method

Activity 2: Runnable Interface

Students develop a program using:

  • Runnable
  • Multiple thread objects
  • Different thread names

Example Output:

Run by Ali
Run by Bob
Run by Jim

Activity 3: Thread Sleep Demonstration

Students create a program that:

  • Uses Thread.sleep()
  • Displays delayed execution

Example:

Thread.sleep(1000);

Activity 4: Joining Threads

Students implement:

  • Parent thread waiting for child thread completion
  • join() method usage

Activity 5: Simulating Race Condition

Students develop a banking withdrawal simulation:

  • Shared bank account
  • Multiple withdrawal threads
  • Observe inconsistent results without synchronization

Activity 6: Synchronization

Students improve the banking simulation by:

  • Adding synchronized methods
  • Preventing race conditions

Activity 7: Producer-Consumer Communication

Students create two threads:

  • Producer thread
  • Consumer thread

Using:

  • wait()
  • notify()

Activity 8: Thread Lifecycle Observation

Students develop a program that:

  • Displays thread states during execution
  • Observes transitions between:
    • NEW
    • RUNNABLE
    • BLOCKED
    • WAITING
    • TIMED_WAITING
    • TERMINATED

📚 Suggested Readings

  1. https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
  2. https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html
  3. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.State.html
  4. https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
  5. https://en.wikipedia.org/wiki/Java_concurrency
  6. https://medium.com/@syed.fawzul.azim/how-java-threads-work-jvm-internals-cpu-level-execution-and-multi-threading-in-java-89d8d343677f
  7. https://www.w3schools.com/java/java_threads.asp

Clone this wiki locally