A comprehensive Java project demonstrating advanced multithreading concepts through two distinct systems:
- Supervisor for Abstract Program Management with automatic restart and state handling
- Thread-safe Message Queue using
java.util.concurrentpackage
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.
| 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 |
- Multithreading:
AbstractProgramandSupervisorrun in separate threads - Synchronization: Uses
synchronized,wait(), andnotifyAll()for thread coordination - Daemon Thread: Inside
AbstractProgram, a daemon thread randomly changes state every 2 seconds - Logging: SLF4J logs all state changes and actions
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
- Two threads start:
AbstractProgramandSupervisor AbstractProgrambegins operation and randomly changes its stateSupervisorcontinuously checks program state:- If
STOPPING→ restarts the program - If
FATAL_ERROR→ stops the program - Otherwise → waits for state change notifications
- If
- All changes are logged to console
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 ...
A fully concurrent message queue system where multiple producer and consumer threads operate simultaneously on a shared blocking queue.
NamedThreadFactory.java # Custom thread factory with meaningful names MessageQueue.java # Main orchestrator class MessageWriter.java # Producer threads MessageReader.java # Consumer threads
- Thread-safe Operations: Uses
LinkedBlockingQueuefromjava.util.concurrent - Meaningful Thread Names: Custom
NamedThreadFactorycreates 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
- 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
- N Writer Threads generate messages with format:
Msg-{id}-{timestamp} - Messages are placed in a shared
BlockingQueuewith capacity limits - N Reader Threads consume messages from the queue
- All operations are logged with thread identifiers for traceability
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 ...
- Invalid thread count arguments
- Queue capacity exhaustion
- Thread interruption during operations
- Memory constraints (OutOfMemoryError)
- Security exceptions
This project demonstrates:
- Multithreading Fundamentals: Thread creation, management, and coordination
- Synchronization Techniques:
synchronized,wait/notify,BlockingQueue - Concurrent Collections: Thread-safe data structures from
java.util.concurrent - Thread Pool Management: ExecutorService configuration and lifecycle
- Error Handling: Graceful degradation and resource cleanup
- Design Patterns: Producer-Consumer, Observer, Factory patterns
Here is the task --->
- 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 launchSTOPPING- stoppedRUNNING- runningFATAL_ERROR- critical error
- 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
- When state is
- All state changes are accompanied by console messages
- Use
wait/notifyconstructs - Do not miss any status of the abstract program
- 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.concurrentpackage - Thread names should be meaningful
- Use of
wait/notifyconstructs is prohibited