private boolean tryDropSpare ( WorkQueue w ) { if ( w != null && w . isEmpty ( ) ) { // no local tasks long c ; int sp , wl ; WorkQueue [ ] ws ; WorkQueue v ; while ( ( short ) ( ( c = ctl ) >> TC_SHIFT ) > 0 && ( ( sp = ( int ) c ) != 0 || ( int ) ( c >> AC_SHIFT ) > 0 ) && ( ws = workQueues ) != null && ( wl = ws . length ) > 0 ) { boolean dropped , canDrop ; if ( sp == 0 ) { // no queued workers long nc = ( ( AC_MASK & ( c - AC_UNIT ) ) | ( TC_MASK & ( c - TC_UNIT ) ) | ( SP_MASK & c ) ) ; dropped = U . compareAndSwapLong ( this , CTL , c , nc ) ; } else if ( ( v = ws [ ( wl - 1 ) & sp ] ) == null || v . scanState != sp ) dropped = false ; // stale; retry else { long nc = v . stackPred & SP_MASK ; if ( w == v || w . scanState >= 0 ) { canDrop = true ; // w unqueued or topmost nc |= ( ( AC_MASK & c ) | // ensure replacement ( TC_MASK & ( c - TC_UNIT ) ) ) ; } else { // w may be queued canDrop = false ; // help uncover nc |= ( ( AC_MASK & ( c + AC_UNIT ) ) | ( TC_MASK & c ) ) ; } if ( U . compareAndSwapLong ( this , CTL , c , nc ) ) { v . scanState = sp & ~ UNSIGNALLED ; LockSupport . unpark ( v . parker ) ; dropped = canDrop ; } else dropped = false ; } if ( dropped ) { // pre-deregister int cfg = w . config , idx = cfg & SMASK ; if ( idx >= 0 && idx < ws . length && ws [ idx ] == w ) ws [ idx ] = null ; w . config = cfg | UNREGISTERED ; w . qlock = - 1 ; return true ; } } } return false ; }
If the given worker is a spare with no queued tasks and there are enough existing workers drops it from ctl counts and sets its state to terminated .