@Override public void start() { ScheduledExecutorService next = null; for (;;) { ScheduledExecutorService current = executor.get(); if (current != SHUTDOWN) { if (next != null) { next.shutdown(); } return; } if (next == null) { next = createExecutor(threadFactory); } if (executor.compareAndSet(current, next)) { return; } } }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * <p>History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * <p>History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * <p>History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
@Override public void start() { ScheduledExecutorService next = null; for (;;) { ScheduledExecutorService current = executor.get(); if (current != SHUTDOWN) { if (next != null) { next.shutdown(); } return; } if (next == null) { next = createExecutor(threadFactory); } if (executor.compareAndSet(current, next)) { return; } } }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * <p>History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
@Override public void start() { ScheduledExecutorService next = null; for (;;) { ScheduledExecutorService current = executor.get(); if (current != SHUTDOWN) { if (next != null) { next.shutdown(); } return; } if (next == null) { next = createExecutor(threadFactory); } if (executor.compareAndSet(current, next)) { return; } } }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.0.5 - experimental */ @Experimental @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
/** * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. */ public SingleScheduler(ThreadFactory threadFactory) { this.threadFactory = threadFactory; executor.lazySet(createExecutor(threadFactory)); }
/** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.0.5 - experimental */ @Experimental @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); }
/** * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. */ public SingleScheduler(ThreadFactory threadFactory) { this.threadFactory = threadFactory; executor.lazySet(createExecutor(threadFactory)); }
@Test public void startRace() { final Scheduler s = new SingleScheduler(); for (int i = 0; i < 500; i++) { s.shutdown(); Runnable r1 = new Runnable() { @Override public void run() { s.start(); } }; TestCommonHelper.race(r1, r1); } }
/** * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. */ public SingleScheduler(ThreadFactory threadFactory) { this.threadFactory = threadFactory; executor.lazySet(createExecutor(threadFactory)); }
@Test public void shutdownRejects() { final int[] calls = { 0 }; Runnable r = new Runnable() { @Override public void run() { calls[0]++; } }; Scheduler s = new SingleScheduler(); s.shutdown(); assertEquals(Disposables.disposed(), s.scheduleDirect(r)); assertEquals(Disposables.disposed(), s.scheduleDirect(r, 1, TimeUnit.SECONDS)); assertEquals(Disposables.disposed(), s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS)); Worker w = s.createWorker(); ((ScheduledWorker)w).executor.shutdownNow(); assertEquals(Disposables.disposed(), w.schedule(r)); assertEquals(Disposables.disposed(), w.schedule(r, 1, TimeUnit.SECONDS)); assertEquals(Disposables.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS)); assertEquals(0, calls[0]); w.dispose(); assertTrue(w.isDisposed()); }