A Java multithreading simulation where trucks unload grain into two elevators simultaneously.
Homework #3 for the Multithreading lesson at AIT TR GmbH.
Rework the Elevator project from classwork so that:
- Trucks unload grain into two elevators instead of one
- During each trip, half the grain goes into elevator #1 and half into elevator #2
- The
Elevator.javaclass cannot be modified - No grain loss β every kernel must be accounted for
- Synchronization must be as efficient as possible (no unnecessary blocking)
- Java
- Multithreading (
Runnable,Thread) - Synchronization (
synchronized,wait/notifyAll) - IntelliJ IDEA
Truck (Thread)
β
βββ half load βββΊ ElevatorWorker #1 (Thread) βββΊ Elevator #1
β
βββ half load βββΊ ElevatorWorker #2 (Thread) βββΊ Elevator #2
Multiple trucks run in parallel. Each truck splits its load exactly in half and notifies both elevator workers. Both halves are unloaded concurrently β neither elevator waits for the other.
Elevator.javais read-only β the synchronization logic lives entirely inTruckand the worker/dispatcher classes- Grain split is exact β if the load is odd, one elevator gets
load/2and the other getsload - load/2(no rounding loss) wait/notifyAllare used to coordinate between trucks and elevator workers without busy-waiting
src/ait/elevator/
βββ Elevator.java # Provided class β cannot be edited
βββ Truck.java # Runnable β generates grain loads, splits them in half
βββ ElevatorWorker.java # Runnable β receives half-loads and adds to its Elevator
βββ Main.java # Entry point β creates trucks, workers, starts threads
git clone https://github.com/AOgit/ait-two-elevators.gitOpen in IntelliJ IDEA and run Main.java.
The console will show each truck's delivery and the running totals in both elevators. At the end, the sum of both elevators equals the total grain delivered by all trucks.