final WorkQueue registerWorker ( ForkJoinWorkerThread wt ) { UncaughtExceptionHandler handler ; wt . setDaemon ( true ) ; // configure thread if ( ( handler = ueh ) != null ) wt . setUncaughtExceptionHandler ( handler ) ; WorkQueue w = new WorkQueue ( this , wt ) ; int i = 0 ; // assign a pool index int mode = config & MODE_MASK ; int rs = lockRunState ( ) ; try { WorkQueue [ ] ws ; int n ; // skip if no array if ( ( ws = workQueues ) != null && ( n = ws . length ) > 0 ) { int s = indexSeed += SEED_INCREMENT ; // unlikely to collide int m = n - 1 ; i = ( ( s << 1 ) | 1 ) & m ; // odd-numbered indices if ( ws [ i ] != null ) { // collision int probes = 0 ; // step by approx half n int step = ( n <= 4 ) ? 2 : ( ( n >>> 1 ) & EVENMASK ) + 2 ; while ( ws [ i = ( i + step ) & m ] != null ) { if ( ++ probes >= n ) { workQueues = ws = Arrays . copyOf ( ws , n <<= 1 ) ; m = n - 1 ; probes = 0 ; } } } w . hint = s ; // use as random seed w . config = i | mode ; w . scanState = i ; // publication fence ws [ i ] = w ; } } finally { unlockRunState ( rs , rs & ~ RSLOCK ) ; } wt . setName ( workerNamePrefix . concat ( Integer . toString ( i >>> 1 ) ) ) ; return w ; }
Callback from ForkJoinWorkerThread constructor to establish and record its WorkQueue .