diff --git a/producer.py b/producer.py new file mode 100644 index 0000000000000000000000000000000000000000..4c3cf2dec048a8338e06cca82ab794e49a3e13bf --- /dev/null +++ b/producer.py @@ -0,0 +1,37 @@ +from car import Car +from buffer import Buffer +import threading +import random +import time + + +class Producer: + def __init__(self, buffer_obj): + self.buffer = buffer_obj + + def produce(self): + while True: + time.sleep(random.uniform(0.5, 2.0)) # Random interval between checks + + with self.buffer.condition: + while self.buffer.full(): + self.buffer.condition.wait() # Wait until buffer is not full + + car_id = random.randint(1, 100) # Generate a random car ID + car = Car(car_id) + self.buffer.push(car) + print("Producer: Car", car.get_car_id(), "parked") + + if self.buffer.empty(): + # Buffer was empty before parking, wake up all sleeping consumers + self.buffer.condition.notify_all() + + +# Example usage: +buffer = Buffer(size=10) # Assuming buffer is already initialized with a size of 10 +producer = Producer(buffer) + +# Create and start the producer thread +producer_thread = threading.Thread(target=producer.produce) +producer_thread.start() +