@@ -52,24 +52,24 @@ public SynchronizerFactoryWithState getNextAvailableSynchronizer() {
5252 synchronized (activeSourceLock ) {
5353 SynchronizerFactoryWithState factory = null ;
5454
55- // There is at least one available factory.
56- if ( synchronizers . stream (). anyMatch ( s -> s . getState () == SynchronizerFactoryWithState . State . Available )) {
55+ int visited = 0 ;
56+ while ( visited < synchronizers . size ( )) {
5757 // Look for the next synchronizer starting at the position after the current one. (avoiding just re-using the same synchronizer.)
58- while (factory == null ) {
59- sourceIndex ++;
60- // We aren't using module here because we want to keep the stored index within range instead
61- // of increasing indefinitely.
62- if (sourceIndex >= synchronizers .size ()) {
63- sourceIndex = 0 ;
64- }
65- SynchronizerFactoryWithState candidate = synchronizers .get (sourceIndex );
66- if (candidate .getState () == SynchronizerFactoryWithState .State .Available ) {
67- factory = candidate ;
68- }
58+ sourceIndex ++;
6959
60+ // We aren't using module here because we want to keep the stored index within range instead
61+ // of increasing indefinitely.
62+ if (sourceIndex >= synchronizers .size ()) {
63+ sourceIndex = 0 ;
7064 }
71- }
7265
66+ SynchronizerFactoryWithState candidate = synchronizers .get (sourceIndex );
67+ if (candidate .getState () == SynchronizerFactoryWithState .State .Available ) {
68+ factory = candidate ;
69+ break ;
70+ }
71+ visited ++;
72+ }
7373 return factory ;
7474 }
7575 }
@@ -80,15 +80,11 @@ public SynchronizerFactoryWithState getNextAvailableSynchronizer() {
8080 */
8181 public boolean isPrimeSynchronizer () {
8282 synchronized (activeSourceLock ) {
83- boolean firstAvailableSynchronizer = true ;
8483 for (int index = 0 ; index < synchronizers .size (); index ++) {
8584 if (synchronizers .get (index ).getState () == SynchronizerFactoryWithState .State .Available ) {
86- if (firstAvailableSynchronizer && sourceIndex == index ) {
87- // This is the first synchronizer that is available, and it also is the current one.
85+ if (sourceIndex == index ) {
8886 return true ;
8987 }
90- // Subsequently encountered synchronizers that are available are not the first one.
91- firstAvailableSynchronizer = false ;
9288 }
9389 }
9490 }
0 commit comments