/** * Common method to enable a {@link VirtualTimeScheduler} in {@link Schedulers} * factories. The supplier is lazily called. Enabling the same scheduler twice is * also idempotent. * * @param schedulerSupplier the supplier executed to obtain a fresh {@link VirtualTimeScheduler} * @param exact whether or not to force the use of the supplier, even if there's a matching scheduler * @return the scheduler that is actually used after the operation. */ static VirtualTimeScheduler enable(Supplier<VirtualTimeScheduler> schedulerSupplier, boolean exact) { for (; ; ) { VirtualTimeScheduler s = CURRENT.get(); if (s != null && !exact) { return s; } VirtualTimeScheduler newS = schedulerSupplier.get(); if (newS == CURRENT.get()) { return newS; //nothing to do, it has already been set in the past } if (CURRENT.compareAndSet(s, newS)) { Schedulers.setFactory(new AllFactory(newS)); if (CURRENT.get() == newS) { return newS; } } } }
/** * Common method to enable a {@link VirtualTimeScheduler} in {@link Schedulers} * factories. The supplier is lazily called. Enabling the same scheduler twice is * also idempotent. * * @param schedulerSupplier the supplier executed to obtain a fresh {@link VirtualTimeScheduler} * @param exact whether or not to force the use of the supplier, even if there's a matching scheduler * @return the scheduler that is actually used after the operation. */ static VirtualTimeScheduler enable(Supplier<VirtualTimeScheduler> schedulerSupplier, boolean exact) { for (; ; ) { VirtualTimeScheduler s = CURRENT.get(); if (s != null && !exact) { return s; } VirtualTimeScheduler newS = schedulerSupplier.get(); if (newS == CURRENT.get()) { return newS; //nothing to do, it has already been set in the past } if (CURRENT.compareAndSet(s, newS)) { Schedulers.setFactory(new AllFactory(newS)); if (CURRENT.get() == newS) { return newS; } } } }