This is my explanation of using arrivalGeneration == generation :
Streams wait at the gateway until both conditions are met: the gate is closed AND the local arrivalGeneration value in their await() methods is equal to the current generation value.
Why do we need the second condition here?
Suppose the first group (generation) of participating threads is waiting for the gateway to open. Their local arrivalCeneration value is 0 , and the value of the current generation instance is also 0 . When the gate begins to open, the generation value increases by one and is now equal to 1 (so that newcomers now receive the local arrivalGeneration value set to 1 ). Then the notifyAll() gate open method is called. Our waiting streams from group 0 take time to wake up. During this time, the gate can be closed again. However, since the second condition arrivalGeneration == generation now false for group 0, they do not continue iteration in the while loop and are allowed to pass through the gate (even if the first condition is true because the gate is officially closed again).
At the same time, those participants from group No. 1 who were unable to slip through the gate before they closed again go into the while loop to wait, because both conditions are fulfilled for them: the gate is closed and their local arrivalGeneration value is 1 which matches the current generation value. The next time the gate starts to open again, the generation value will be updated to 2 , and the stream from group # 1 will be able to complete their passage through the gate, etc.
source share