Skip to content
Snippets Groups Projects
Commit a1cf3d6e authored by Thomas Letzgus's avatar Thomas Letzgus
Browse files

Upload New File

parent 062a32fd
No related branches found
No related tags found
No related merge requests found
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment