/** * Return whether this throttle is currently active. * @return {@code true} if the concurrency limit for this instance is active * @see #getConcurrencyLimit() * @see #setConcurrencyLimit */ public final boolean isThrottleActive() { return this.concurrencyThrottle.isThrottleActive(); }
/** * Create a new instance of the {@code AsyncRestTemplate} using default settings. * <p>This constructor uses a {@link SimpleClientHttpRequestFactory} in combination * with a {@link SimpleAsyncTaskExecutor} for asynchronous execution. */ public AsyncRestTemplate() { this(new SimpleAsyncTaskExecutor()); }
/** * Executes the given task, within a concurrency throttle * if configured (through the superclass's settings). * @see #doExecute(Runnable) */ @Override public void execute(Runnable task) { execute(task, TIMEOUT_INDEFINITE); }
@Test public void threadFactoryOverridesDefaults() throws Exception { final Object monitor = new Object(); SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "test"); } }); ThreadNameHarvester task = new ThreadNameHarvester(monitor); executeAndWait(executor, task, monitor); assertEquals("test", task.getThreadName()); }
@Override public void execute(Runnable command) { this.taskExecutor.execute(command); }
public BootstrapSessionFactoryInvocationHandler(AsyncTaskExecutor bootstrapExecutor) { this.sessionFactoryFuture = bootstrapExecutor.submit( (Callable<SessionFactory>) LocalSessionFactoryBuilder.this::buildSessionFactory); }
/** * Executes the given task, within a concurrency throttle * if configured (through the superclass's settings). * <p>Executes urgent tasks (with 'immediate' timeout) directly, * bypassing the concurrency throttle (if active). All other * tasks are subject to throttling. * @see #TIMEOUT_IMMEDIATE * @see #doExecute(Runnable) */ @Override public void execute(Runnable task, long startTimeout) { Assert.notNull(task, "Runnable must not be null"); Runnable taskToUse = (this.taskDecorator != null ? this.taskDecorator.decorate(task) : task); if (isThrottleActive() && startTimeout > TIMEOUT_IMMEDIATE) { this.concurrencyThrottle.beforeAccess(); doExecute(new ConcurrencyThrottlingRunnable(taskToUse)); } else { doExecute(taskToUse); } }
/** * Return the maximum number of parallel accesses allowed. */ public final int getConcurrencyLimit() { return this.concurrencyThrottle.getConcurrencyLimit(); }
/** * Set the maximum number of parallel accesses allowed. * -1 indicates no concurrency limit at all. * <p>In principle, this limit can be changed at runtime, * although it is generally designed as a config time setting. * NOTE: Do not switch between -1 and any concrete limit at runtime, * as this will lead to inconsistent concurrency counts: A limit * of -1 effectively turns off concurrency counting completely. * @see #UNBOUNDED_CONCURRENCY */ public void setConcurrencyLimit(int concurrencyLimit) { this.concurrencyThrottle.setConcurrencyLimit(concurrencyLimit); }
@Override public void run() { try { this.target.run(); } finally { concurrencyThrottle.afterAccess(); } } }
@Override public <T> Future<T> submit(Callable<T> task) { submitStartCounter++; Future<T> future = super.submit(task); submitCompleteCounter++; synchronized (this) { notifyAll(); } return future; }
public NoOpScheduledExecutorTask() { super(new NoOpRunnable()); } }
@Test public void threadNameGetsSetCorrectly() throws Exception { final String customPrefix = "chankPop#"; final Object monitor = new Object(); SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(customPrefix); ThreadNameHarvester task = new ThreadNameHarvester(monitor); executeAndWait(executor, task, monitor); assertThat(task.getThreadName(), startsWith(customPrefix)); }
@Override public void execute(Runnable task) { this.taskExecutor.execute(task); }
@SuppressWarnings("deprecation") @Override protected AsyncClientHttpRequestFactory createRequestFactory() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); AsyncListenableTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); requestFactory.setTaskExecutor(taskExecutor); return requestFactory; }
@Override public Future<?> submit(Runnable task) { FutureTask<Object> future = new FutureTask<>(task, null); execute(future, TIMEOUT_INDEFINITE); return future; }
@Override public <T> Future<T> submit(Callable<T> task) { FutureTask<T> future = new FutureTask<>(task); execute(future, TIMEOUT_INDEFINITE); return future; }
@Override public ListenableFuture<?> submitListenable(Runnable task) { ListenableFutureTask<Object> future = new ListenableFutureTask<>(task, null); execute(future, TIMEOUT_INDEFINITE); return future; }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { ListenableFutureTask<T> future = new ListenableFutureTask<>(task); execute(future, TIMEOUT_INDEFINITE); return future; }
private void executeAndWait(SimpleAsyncTaskExecutor executor, Runnable task, Object monitor) { synchronized (monitor) { executor.execute(task); try { monitor.wait(); } catch (InterruptedException ignored) { } } }