Skip to content

Implementation of two multithreading patterns: 1. Supervisor program with state management using `wait/notify` and 2. Message queue with producer-consumer using `java.util.concurrent` . A practical demonstration of thread coordination and synchronization.

Notifications You must be signed in to change notification settings

fa11astro/JavaExamTasks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Multithreaded Supervisor & Message Queue System in Java

A comprehensive Java project demonstrating advanced multithreading concepts through two distinct systems:

  1. Supervisor for Abstract Program Management with automatic restart and state handling
  2. Thread-safe Message Queue using java.util.concurrent package

Part 1: Supervisor for Abstract Program

Description

A robust supervisor system that monitors and controls an abstract program running in a separate thread. The program randomly changes its state, and the supervisor reacts accordingly.

Program States

State Description Supervisor Action
UNKNOWN Before first launch Starts the program
RUNNING Program is working Continues monitoring
STOPPING Program stopped Automatically restarts
FATAL_ERROR Critical error Shuts down the program

Technical Implementation

  • Multithreading: AbstractProgram and Supervisor run in separate threads
  • Synchronization: Uses synchronized, wait(), and notifyAll() for thread coordination
  • Daemon Thread: Inside AbstractProgram, a daemon thread randomly changes state every 2 seconds
  • Logging: SLF4J logs all state changes and actions

Code Structure

AbstractProgram.java # Abstract program with state-changing daemon
Supervisor.java # Supervisor monitoring and control
ProgramState.java # Enum with possible program states
Main.java # Entry point launching both threads

Workflow

  1. Two threads start: AbstractProgram and Supervisor
  2. AbstractProgram begins operation and randomly changes its state
  3. Supervisor continuously checks program state:
    • If STOPPING → restarts the program
    • If FATAL_ERROR → stops the program
    • Otherwise → waits for state change notifications
  4. All changes are logged to console

Example Supervisor Output

Starting program for the first time.
Daemon thread started
Supervisor started monitoring
Program state changed to: RUNNING
Supervisor detected state: RUNNING
Program state changed to: STOPPING
Supervisor detected state: STOPPING
Restarting program...
Program state changed to: RUNNING
...

Part 2: Thread-safe Message Queue System

Description

A fully concurrent message queue system where multiple producer and consumer threads operate simultaneously on a shared blocking queue.

System Components

NamedThreadFactory.java # Custom thread factory with meaningful names
MessageQueue.java # Main orchestrator class
MessageWriter.java # Producer threads
MessageReader.java # Consumer threads

Key Features

  • Thread-safe Operations: Uses LinkedBlockingQueue from java.util.concurrent
  • Meaningful Thread Names: Custom NamedThreadFactory creates descriptive thread names
  • Graceful Shutdown: Proper thread pool termination with timeout handling
  • Error Handling: Comprehensive exception handling and error reporting
  • Configurable: Number of threads specified via command line argument

Configuration & Parameters

  • Queue Capacity: 100 messages
  • Producer Delay: 200-800 ms random delay
  • Consumer Delay: 300-900 ms random delay
  • Operation Timeout: 1 second for queue operations
  • Runtime: Program runs for 10 seconds by default

Message Flow

  1. N Writer Threads generate messages with format: Msg-{id}-{timestamp}
  2. Messages are placed in a shared BlockingQueue with capacity limits
  3. N Reader Threads consume messages from the queue
  4. All operations are logged with thread identifiers for traceability

Example Queue Output

Sent by Writer-0: Msg-0-1648579321000
Received by Reader-0: Msg-0-1648579321000
Sent by Writer-1: Msg-1-1648579321200
Reader-2: queue is empty
Received by Reader-1: Msg-1-1648579321200
Writer-3: queue is full
...

Error Conditions Handled

  • Invalid thread count arguments
  • Queue capacity exhaustion
  • Thread interruption during operations
  • Memory constraints (OutOfMemoryError)
  • Security exceptions

Learning Objectives

This project demonstrates:

  1. Multithreading Fundamentals: Thread creation, management, and coordination
  2. Synchronization Techniques: synchronized, wait/notify, BlockingQueue
  3. Concurrent Collections: Thread-safe data structures from java.util.concurrent
  4. Thread Pool Management: ExecutorService configuration and lifecycle
  5. Error Handling: Graceful degradation and resource cleanup
  6. Design Patterns: Producer-Consumer, Observer, Factory patterns

Here is the task --->

Multithreading Programming Assignment

1. Supervisor for Managing an Abstract Program

Requirements:

  • Create a supervisor (control program) that monitors the execution of an abstract program
  • The abstract program runs in a separate thread
  • Use states of enumerated type:
    • UNKNOWN - before first launch
    • STOPPING - stopped
    • RUNNING - running
    • FATAL_ERROR - critical error

Implementation:

  • A daemon thread randomly changes the program's state at regular intervals
  • The supervisor has methods to stop and start the abstract program
  • The supervisor cyclically polls the abstract program:
    • When state is STOPPING → restarts the program
    • When state is FATAL_ERROR → terminates the program
  • All state changes are accompanied by console messages
  • Use wait/notify constructs
  • Do not miss any status of the abstract program

2. Message Queue with Multithreaded Access

Requirements:

  • Create a message queue
  • N threads should write to the queue (number specified via args)
  • N threads should read from the queue (number specified via args)
  • Use only the java.util.concurrent package
  • Thread names should be meaningful
  • Use of wait/notify constructs is prohibited

About

Implementation of two multithreading patterns: 1. Supervisor program with state management using `wait/notify` and 2. Message queue with producer-consumer using `java.util.concurrent` . A practical demonstration of thread coordination and synchronization.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages