/** * Only accepts if the queue size is not exceeded. * * @return true if the task was accepted. */ @Override public <R> Task<R> offerJob(T key, Supplier<Task<R>> job, int maxQueueSize) { // todo remove this. if (key == null) { executorService.execute(() -> InternalUtils.safeInvoke(job)); } return getSerializer(key).executeSerialized(() -> InternalUtils.safeInvoke(job), maxQueueSize); }
public <R> Task<R> execute(Supplier<Task<R>> taskSupplier) { final Task<R> completion = new Task<>(); if ((maxQueueSize > 0 && queueSize >= maxQueueSize) || !queue.add(() -> { Task<R> source = InternalUtils.safeInvoke(taskSupplier); InternalUtils.linkFutures(source, completion); return source; })) { throw new IllegalStateException(String.format("Queue full (%d > %d)", queue.size(), maxQueueSize)); } queueSizeUpdater.incrementAndGet(this); tryDrainQueue(); return completion; }
@Override public <R> Task<R> executeSerialized(Supplier<Task<R>> taskSupplier, int maxQueueSize) { final Task<R> completion = new Task<>(); int queueSize = size; if (DEBUG_ENABLED && queueSize >= maxQueueSize / 10) { logger.debug("Queued " + queueSize + " / " + maxQueueSize + " for " + key); } if (queueSize >= maxQueueSize || !queue.add(() -> { Task<R> source = InternalUtils.safeInvoke(taskSupplier); InternalUtils.linkFutures(source, completion); return source; })) { return Task.fromException(new IllegalStateException(String.format("Queue full for %s (%d > %d)", key, queue.size(), maxQueueSize))); } // managing the size like this to avoid using ConcurrentLinkedQueue.size() sizeUpdater.incrementAndGet(this); tryExecute(false); return completion; }
private <R> Task<R> doRunInternal( ActorEntry<T> entry1, LocalObjects.LocalObjectEntry<T> entry2, final TaskFunction<LocalObjects.LocalObjectEntry<T>, R> function) { try { Task<R> res = InternalUtils.safeInvoke(() -> function.apply(entry2)); // the await here is to ensure that the finally block executed at the right time. await(res); return res; } finally { if (isDeactivated() || entry1 != entry2) { await(entry1.deactivate()); } else { push(entry1); } } }
if (entry != null) final Task<Object> result = InternalUtils.safeInvoke(() -> entry.run( target -> performInvocation(ctx, invocation, entry, target))); InternalUtils.safeInvoke(() -> executionSerializer.offerJob(toReference, () -> onActivate(ctx, invocation), maxQueueSize));