@Override public void run() { addThread(); verifyNotNull(r).run(); removeThread(); } }, threadName);
new AffinityGroupThreadFactory(NAME, handler); assertThat(getReservations()).isEmpty(); final Thread t0 = fac.newThread(new SleeperRunnable()); final Thread t1 = fac.newThread(new SleeperRunnable()); final Thread t2 = fac.newThread(new SleeperRunnable());
void initExecutor() { if (!executor.isPresent() && mode != Mode.UNKNOWN) { final ThreadFactory factory; final String newName = String.format("%s-%s", Thread.currentThread().getName(), getClass().getSimpleName()); if (threadGroupingEnabled) { factory = new AffinityGroupThreadFactory(newName, manager); } else { factory = new AffinityThreadFactory(newName, AffinityStrategies.ANY); } final int threads; if (threadPoolSize == 0) { if (mode == Mode.SINGLE_MODE) { threads = DEFAULT_NUM_THREADS_IN_SINGLE_MODE; } else { threads = Runtime.getRuntime().availableProcessors(); } } else { threads = threadPoolSize; } LOGGER.trace("Create executor with {} threads and factory {}.", threads, factory); executor = Optional.of(MoreExecutors.listeningDecorator( Executors.newFixedThreadPool(threads, factory))); } }
synchronized void addThread() { if (helperThread == null) { startHelperThread(); } numThreads.incrementAndGet(); LOGGER.info("Starting {}.", Thread.currentThread().getName()); final AffinityLock lock = verifyNotNull(affinityLock); checkState(lock.isAllocated(), "Failed to allocate lock: %s.", lock); Affinity.setAffinity(lock.cpuId()); }