final void registerWorker ( WorkQueue w ) { Mutex lock = this . lock ; lock . lock ( ) ; try { WorkQueue [ ] ws = workQueues ; if ( w != null && ws != null ) { // skip on shutdown/failure int rs , n = ws . length , m = n - 1 ; int s = nextSeed += SEED_INCREMENT ; // rarely-colliding sequence w . seed = ( s == 0 ) ? 1 : s ; // ensure non-zero seed int r = ( s << 1 ) | 1 ; // use odd-numbered indices if ( ws [ r &= m ] != null ) { // collision int probes = 0 ; // step by approx half size int step = ( n <= 4 ) ? 2 : ( ( n >>> 1 ) & SQMASK ) + 2 ; while ( ws [ r = ( r + step ) & m ] != null ) { if ( ++ probes >= n ) { workQueues = ws = Arrays . copyOf ( ws , n <<= 1 ) ; m = n - 1 ; probes = 0 ; } } } w . eventCount = w . poolIndex = r ; // establish before recording ws [ r ] = w ; // also update seq runState = ( ( rs = runState ) & SHUTDOWN ) | ( ( rs + 2 ) & ~ SHUTDOWN ) ; } } finally { lock . unlock ( ) ; } }
Callback from ForkJoinWorkerThread constructor to establish its poolIndex and record its WorkQueue . To avoid scanning bias due to packing entries in front of the workQueues array we treat the array as a simple power - of - two hash table using per - thread seed as hash expanding as needed .