Skip to content
ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Java Concurrent Car Wash Simulation

This project is a multithreaded Java simulation of a concurrent car wash service station. It implements a classic solution to the Producer-Consumer Problem using a bounded buffer and custom semaphores to manage thread safety and resource allocation.

The core logic was developed as part of the CS241: Operating System curriculum at Cairo University, Faculty of Computers and Artificial Intelligence.


๐Ÿ‘ฅ Contributors

The members and their specific contributions are listed below.

Name Contribution
George Ezzat Hosni Implemented ServiceStation (Main class, input, and thread orchestration).
Doha Fathy Refaey Implemented the custom Semaphore class (using wait()/notify()).
Nagham Sabry Ahmed Implemented the Pump (Consumer) thread logic.
Marym Ali Abdelkarym Implemented the Car (Producer) thread logic.

โš™๏ธ Project Overview

The simulation models a service station with a limited number of service bays and a fixed-size waiting area.

  • Cars (Producers): Arrive at the station as Car threads.

  • Waiting Area (Bounded Buffer): Cars enter a waitingQueue (the buffer). If the queue is full, the Car thread blocks until a spot is free.

  • Pumps (Consumers): A fixed number (N) of Pump threads run concurrently. They block if the queue is empty (full.semaphoreWait()).

  • Service Bays (Resource Semaphore): After a Pump takes a car from the queue, it must also acquire a free service bay. A separate pumps semaphore manages this, ensuring only N cars can be serviced at the same time.


๐Ÿ—๏ธ Concurrency Design & Architecture

The core of this project is the correct management of two distinct shared resources: the waiting spots in the queue and the service bays themselves.

This is achieved using four semaphores, all custom-built using Java's wait() and notify().

Semaphore Initial Value Purpose
mutex 1 Mutual Exclusion: A binary semaphore that ensures only one thread (Car or Pump) can access the waitingQueue at a time.
empty waitingAreaCapacity Tracks empty spots: A counting semaphore that tracks the number of free spots in the queue. Car threads wait() on this.
full 0 Tracks items: A counting semaphore that tracks the number of cars in the queue. Pump threads wait() on this.
pumps numPumps Tracks resources: A counting semaphore that tracks the number of available service bays. Pump threads wait() on this after taking a car from the queue.

Program Shutdown

The Pump threads are set as daemon threads (pumpThread.setDaemon(true)). This is a critical design choice that allows the simulation to shut down cleanly and automatically once the main thread and all (non-daemon) Car threads have finished executing.


๐Ÿ›๏ธ Project Structure

The application is contained within a single ServiceStation.java file, which includes four main classes:

  • ServiceStation: The main class. It handles user input, initializes all semaphores and the shared queue, creates and starts the Pump and Car threads.

  • Semaphore: A custom-built semaphore class that implements the semaphoreWait() (P) and semaphoreSignal() (V) operations using synchronized, wait(), and notify().

  • Car (Producer): A Runnable class that represents an arriving car. Its run() method implements the producer logic: wait for an empty spot, lock the queue, add itself, unlock the queue, and signal that the queue is no longer empty.

  • Pump (Consumer): A Runnable class that represents a service bay. Its run() method implements the consumer logic: wait for a full spot, lock the queue, take a car, unlock the queue, signal that a spot is now empty, wait for a free pump, service the car, and finally release the pump.


About

A multithreaded car wash simulation in Java that implements the classic producer-consumer pattern. This project demonstrates thread-safe resource management using Semaphores and a Mutex to coordinate concurrent access to a bounded waiting queue and a pool of service bays.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages