sclass QPool { replace Q with CompactQ. int max = numberOfCores(); new SelfNotifyingSet used; new SelfNotifyingSet free; synchronized int total() { ret l(used)+l(free); } synchronized bool nonFree() { ret empty(free) && max < total(); } event customerMustWaitAlert; void fireCustomerMustWaitAlert { vmBus_send customerMustWaitAlert(this, currentQ()); customerMustWaitAlert(); } synchronized WithAutoCloseable acquireQ() { if (nonFree()) fireCustomerMustWaitAlert(); if (total() < max) used.add(newQ()); sleepWhileEmpty(free); Q t = assertNotNull(popFirst(free)); used.add(t); ret withAutoCloseable(-> giveQBack(t), t); } synchronized void giveQBack(Q t) { used.remove(t); free.add(t); } static Q newQ() { ret new Q("Q Pool Inhabitant " + n2(total()+1)) { }; } }