-
Notifications
You must be signed in to change notification settings - Fork 1
Topic 08: Multithreaded Programming
Zhamri Che Ani edited this page May 6, 2026
·
1 revision
To introduce students to multithreading concepts in Java, including thread creation, thread lifecycle management, synchronization, and thread coordination for developing concurrent and efficient applications.
Students should be able to:
- Explain the concepts of processes, threads, and concurrency.
- Differentiate between single-threaded and multithreaded applications.
- Define and create threads using:
-
Threadclass -
Runnableinterface - Lambda expressions
-
- Start and manage multiple threads in a Java application.
- Describe the thread lifecycle and thread states.
- Apply methods that influence thread execution such as:
sleep()yield()join()
- Explain race conditions and critical sections in concurrent programming.
- Implement thread synchronization using:
-
synchronizedmethods -
synchronizedblocks
-
- Coordinate communication between threads using:
wait()notify()notifyAll()
- Develop simple multithreaded Java applications involving shared resources.
- Introduction to Processes and Threads
- Process vs thread
- Concurrency and multitasking
- Context switching
- Lightweight processes
- Single-thread vs Multi-thread Applications
- Advantages of multithreading
- Resource sharing
- Improved responsiveness
- Java Threads
- JVM main thread
- Thread call stack
- Thread execution flow
- Defining Threads
Extending the
Threadclass
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");- Creating and Starting Threads
- Thread constructors
- start() vs run()
- Multiple thread execution
- Thread Lifecycle and States
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
- Influencing Thread Execution
- sleep(milliseconds)
- yield()
- join()
- Thread scheduling
- Race Conditions and Critical Sections
- Shared resources
- Data inconsistency problems
- Mutual exclusion
- Thread Synchronization Synchronized Methods
public synchronized void withdraw(int amt) {
// critical section
}Synchronized Blocks
synchronized(this) {
// critical section
}- Thread Communication
wait()notify()notifyAll()- Monitor locks
- Exception Handling in Threads
- InterruptedException
- Illegal monitor state issues
Students create a Java program that:
- Extends the
Threadclass - Displays thread messages using the
run()method
Students develop a program using:
Runnable- Multiple thread objects
- Different thread names
Example Output:
Run by Ali
Run by Bob
Run by Jim
Students create a program that:
- Uses
Thread.sleep() - Displays delayed execution
Example:
Thread.sleep(1000);Students implement:
- Parent thread waiting for child thread completion
-
join()method usage
Students develop a banking withdrawal simulation:
- Shared bank account
- Multiple withdrawal threads
- Observe inconsistent results without synchronization
Students improve the banking simulation by:
- Adding
synchronizedmethods - Preventing race conditions
Students create two threads:
- Producer thread
- Consumer thread
Using:
wait()notify()
Students develop a program that:
- Displays thread states during execution
- Observes transitions between:
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
- https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html
- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.State.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
- https://en.wikipedia.org/wiki/Java_concurrency
- https://medium.com/@syed.fawzul.azim/how-java-threads-work-jvm-internals-cpu-level-execution-and-multi-threading-in-java-89d8d343677f
- https://www.w3schools.com/java/java_threads.asp