I'm trying to simulate a DVD production line in Java. The thing I have a problem with is the calculation of the time in this kind of simulation and the number of DVDs per hour.

The production line consists of 4 machines, each of which spends a certain random time working on 1 DVD. The third one can work on more than 1 DVD at the same time (max. 100). Also, the third one will always accept more DVDs to work on, as long it's capacity is < 50%, which I plan to label as `m3Capacity`. But for now, let's just ignore the capacity characteristic.

What I have so far is that, with a random number generator (since each machine has random DVD processing time each time it starts working on a new DVD), I assigned a random processing time to each of the four machines, summed up their processing times for the total time spent in one-go production (`totalProdTime = processTime1 + processTime2 + processTime3 + processTime4`), and then I keep summing up those *totalProdTime* values until the total time of the whole simulation reaches the desired duration of the simulation specified by the user (`totalProdTimeSim = totalProdTimeSim + mTotal.totalProdTime`). Later I just verify whether the *totalProdTimeSim* has reached the desired time and that's it.

Now, the problem is that, this way, the production of the DVDs waits for one instance of production to finish all the way until the very end of the machine 4, and then the work begins on the second DVD, etc. What I want to do is that after machine 1 finishes it's work, and the second one begins its work, machine 1 begins work on another DVD with it's newly generated time. The same goes for all other machines. After machine 2 finishes work on a DVD, it immediately begins working on another DVD.

Now, since all machines have random processing times each time they start working on another DVD, it will often happen that one machine 'wants to work on another DVD' while the previous one hasn't finished its work on the current one. In this case, the idle machine just 'waits' for the previous machine to finish processing and then accepts the DVD from the previous machine.

So, in my example, we have that all the machines work one by one, but I want them all to work constantly, even after they finish working on the current DVD (except in the first iteration, where the further machines need to wait for the previous ones to forward them their DVD they previously worked on).

How do you think I should calculate the total simulation time and DVD output? I've been thinking last night about it, but haven't gotten any ideas.

My sketch of how it should work can be found in the picture below. In the sketch all machines behave as if they constantly have something to work on, but should wait from the input from the previous machine.

![Sample sketch](https://i.stack.imgur.com/azCjN.png)

Thanks in advance!