ScheduledFuture<?> f = executor.get().getThreadPool().scheduleAtFixedRate(r, listener.getIntervalTimeInMilliseconds(), listener.getIntervalTimeInMilliseconds(), TimeUnit.MILLISECONDS); return new TimerReference(listener, f);
/** * Since we allow resetting the timer (shutting down the thread) we need to lazily re-start it if it starts being used again. * <p> * This does the lazy initialization and start of the thread in a thread-safe manner while having little cost the rest of the time. */ protected void startThreadIfNeeded() { // create and start thread if one doesn't exist while (executor.get() == null || ! executor.get().isInitialized()) { if (executor.compareAndSet(null, new ScheduledExecutor())) { // initialize the executor that we 'won' setting executor.get().initialize(); } } }
ScheduledFuture<?> f = executor.get().getThreadPool().scheduleAtFixedRate(r, listener.getIntervalTimeInMilliseconds(), listener.getIntervalTimeInMilliseconds(), TimeUnit.MILLISECONDS); return new TimerReference(listener, f);
/** * Since we allow resetting the timer (shutting down the thread) we need to lazily re-start it if it starts being used again. * <p> * This does the lazy initialization and start of the thread in a thread-safe manner while having little cost the rest of the time. */ protected void startThreadIfNeeded() { // create and start thread if one doesn't exist while (executor.get() == null || ! executor.get().isInitialized()) { if (executor.compareAndSet(null, new ScheduledExecutor())) { // initialize the executor that we 'won' setting executor.get().initialize(); } } }
/** * Clears all listeners. * <p> * NOTE: This will result in race conditions if {@link #addTimerListener(com.netflix.hystrix.util.HystrixTimer.TimerListener)} is being concurrently called. * </p> */ public static void reset() { ScheduledExecutor ex = INSTANCE.executor.getAndSet(null); if (ex != null && ex.getThreadPool() != null) { ex.getThreadPool().shutdownNow(); } }
@Test public void testThreadPoolSizeConfiguredWithBuilder() { HystrixTimerThreadPoolProperties.Setter builder = HystrixTimerThreadPoolProperties.Setter().withCoreSize(1); final HystrixTimerThreadPoolProperties props = new HystrixTimerThreadPoolProperties(builder) { }; HystrixPropertiesStrategy strategy = new HystrixPropertiesStrategy() { @Override public HystrixTimerThreadPoolProperties getTimerThreadPoolProperties() { return props; } }; HystrixPlugins.getInstance().registerPropertiesStrategy(strategy); HystrixTimer hystrixTimer = HystrixTimer.getInstance(); hystrixTimer.startThreadIfNeeded(); assertEquals(1, hystrixTimer.executor.get().getThreadPool().getCorePoolSize()); }
/** * Clears all listeners. * <p> * NOTE: This will result in race conditions if {@link #addTimerListener(com.netflix.hystrix.util.HystrixTimer.TimerListener)} is being concurrently called. * </p> */ public static void reset() { ScheduledExecutor ex = INSTANCE.executor.getAndSet(null); if (ex != null && ex.getThreadPool() != null) { ex.getThreadPool().shutdownNow(); } }
@Test public void testThreadPoolSizeDefault() { HystrixTimer hystrixTimer = HystrixTimer.getInstance(); hystrixTimer.startThreadIfNeeded(); assertEquals(Runtime.getRuntime().availableProcessors(), hystrixTimer.executor.get().getThreadPool().getCorePoolSize()); }