public ListenableFuture<T> createNewListener() { SettableFuture<T> listener = SettableFuture.create(); synchronized (listeners) { listeners.add(listener); } // remove the listener when the future completes listener.addListener( () -> { synchronized (listeners) { listeners.remove(listener); } }, directExecutor()); return listener; }
public Partition( int consumersCount, int partitionNumber, IntFunction<ListenableFuture<T>> loader, ListenableFuture<?> previousReleased, IntConsumer disposer) { this.partitionNumber = partitionNumber; this.requested = SettableFuture.create(); this.loaded = Futures.transformAsync( allAsList(requested, previousReleased), ignored -> loader.apply(partitionNumber), directExecutor()); this.released = SettableFuture.create(); released.addListener(() -> disposer.accept(partitionNumber), directExecutor()); this.pendingReleases = consumersCount; }
public synchronized ListenableFuture<?> waitForMinimumWorkers() { if (currentCount >= executionMinCount) { return immediateFuture(null); } SettableFuture<?> future = SettableFuture.create(); futures.add(future); // if future does not finish in wait period, complete with an exception ScheduledFuture<?> timeoutTask = executor.schedule( () -> { synchronized (this) { future.setException(new PrestoException( GENERIC_INSUFFICIENT_RESOURCES, format("Insufficient active worker nodes. Waited %s for at least %s workers, but only %s workers are active", executionMaxWait, executionMinCount, currentCount))); } }, executionMaxWait.toMillis(), MILLISECONDS); // remove future if finished (e.g., canceled, timed out) future.addListener(() -> { timeoutTask.cancel(true); removeFuture(future); }, executor); return future; }
public void submit(HttpForwardActionResult future, Runnable command, boolean synchronous) { if (future != null) { if (synchronous) { try { future.getHttpResponse().get(ConfigurationProperties.maxSocketTimeout(), MILLISECONDS); } catch (TimeoutException e) { future.getHttpResponse().setException(new SocketCommunicationException("Response was not received after " + ConfigurationProperties.maxSocketTimeout() + " milliseconds, to make the proxy wait longer please use \"mockserver.maxSocketTimeout\" system property or ConfigurationProperties.maxSocketTimeout(long milliseconds)", e.getCause())); } catch (InterruptedException | ExecutionException ex) { future.getHttpResponse().setException(ex); } command.run(); } else { future.getHttpResponse().addListener(command, getScheduler()); } } }
ListenableFuture<List<String>> compound = successfulAsList(future1, future2); future1.addListener( new Runnable() { @Override
@Override public void addListener(Runnable listener, Executor executor) { future.addListener(listener, executor); }
public ListenableFuture<T> createNewListener() { SettableFuture<T> listener = SettableFuture.create(); synchronized (listeners) { listeners.add(listener); } // remove the listener when the future completes listener.addListener( () -> { synchronized (listeners) { listeners.remove(listener); } }, directExecutor()); return listener; }
public ListenableFuture<T> createNewListener() { SettableFuture<T> listener = SettableFuture.create(); synchronized (listeners) { listeners.add(listener); } // remove the listener when the future completes listener.addListener( () -> { synchronized (listeners) { listeners.remove(listener); } }, directExecutor()); return listener; }
@Override public void addListener(Runnable listener, Executor executor) { getWrappedFuture().addListener(listener, executor); }
@Override public void addListener(Runnable listener, Executor executor) { getWrappedFuture().addListener(listener, executor); }
@Override public void addListener(Runnable listener, Executor executor) { getWrappedFuture().addListener(listener, executor); }
@Override public void addListener(Runnable listener, Executor executor) { getWrappedFuture().addListener(listener, executor); }
public Partition( int consumersCount, int partitionNumber, IntFunction<ListenableFuture<T>> loader, ListenableFuture<?> previousReleased, IntConsumer disposer) { this.partitionNumber = partitionNumber; this.requested = SettableFuture.create(); this.loaded = Futures.transformAsync( allAsList(requested, previousReleased), ignored -> loader.apply(partitionNumber), directExecutor()); this.released = SettableFuture.create(); released.addListener(() -> disposer.accept(partitionNumber), directExecutor()); this.pendingReleases = consumersCount; }
ListenableFuture<Optional<File>> addDownloadListener(FileReference fileReference, Runnable runnable) { synchronized (downloads) { FileReferenceDownload download = downloads.get(fileReference); if (download != null) { download.future().addListener(runnable, downloadExecutor); return download.future(); } } return null; }
public Partition( int consumersCount, int partitionNumber, IntFunction<ListenableFuture<T>> loader, ListenableFuture<?> previousReleased, IntConsumer disposer) { this.partitionNumber = partitionNumber; this.requested = SettableFuture.create(); this.loaded = Futures.transformAsync( allAsList(requested, previousReleased), ignored -> loader.apply(partitionNumber), directExecutor()); this.released = SettableFuture.create(); released.addListener(() -> disposer.accept(partitionNumber), directExecutor()); this.pendingReleases = consumersCount; }
@Override public ListenableFuture<?> subscribe(final String eventName, final EventListener listener) { final SettableFuture<?> future = SettableFuture.create(); boolean doSubscribe = register(eventName, listener); if (doSubscribe) { doSubscribe(eventName, future); } else { future.set(null); } future.addListener(new Runnable() { @Override public void run() { try { future.get(); } catch (Exception e) { unsubscribe(eventName, listener); disconnect(); } } }, executorService); return future; }
@Benchmark public void benchmark() throws Exception { Semaphore semaphore = new Semaphore(futureCount); ArrayList<SettableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < futureCount; i++) { SettableFuture<?> future = SettableFuture.create(); future.addListener(() -> semaphore.release(1), directExecutor()); futures.add(future); } ListenableFuture<?> anyComplete = whenAnyCompleteCancelOthers(futures); futures.get(futureCount / 2).set(null); semaphore.acquireUninterruptibly(futureCount); anyComplete.get(); }
@Benchmark public void benchmark() throws Exception { Semaphore semaphore = new Semaphore(futureCount); ArrayList<SettableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < futureCount; i++) { SettableFuture<?> future = SettableFuture.create(); future.addListener(() -> semaphore.release(1), directExecutor()); futures.add(future); } ListenableFuture<?> anyComplete = whenAnyCompleteCancelOthers(futures); futures.get(futureCount / 2).set(null); semaphore.acquireUninterruptibly(futureCount); anyComplete.get(); }
public void submit(HttpForwardActionResult future, Runnable command, boolean synchronous) { if (future != null) { if (synchronous) { try { future.getHttpResponse().get(ConfigurationProperties.maxSocketTimeout(), MILLISECONDS); } catch (TimeoutException e) { future.getHttpResponse().setException(new SocketCommunicationException("Response was not received after " + ConfigurationProperties.maxSocketTimeout() + " milliseconds, to make the proxy wait longer please use \"mockserver.maxSocketTimeout\" system property or ConfigurationProperties.maxSocketTimeout(long milliseconds)", e.getCause())); } catch (InterruptedException | ExecutionException ex) { future.getHttpResponse().setException(ex); } command.run(); } else { future.getHttpResponse().addListener(command, getScheduler()); } } }
protected Future<?> schedulePing(final EventListener listener, final MessageWriter writer, final AtomicBoolean disconnect) { final SettableFuture<?> future = SettableFuture.create(); retryTimeout.submit(new Retry(API_MAX_PINGS.get(), API_SUB_PING_INVERVAL.get(), future, new Runnable() { @Override public void run() { if (disconnect.get()) { unsubscribe(disconnect, writer, listener); future.setException(new CancelRetryException()); throw new CancelRetryException(); } listener.onEvent(new Ping()); } })); future.addListener(new Runnable() { @Override public void run() { try { future.get(); } catch (InterruptedException e) { unsubscribe(disconnect, writer, listener); } catch (ExecutionException e) { unsubscribe(disconnect, writer, listener); } } }, executorService); return future; }