/** * @see Queue#isEmpty() */ protected boolean hasTasks() { assert inEventLoop(); return !taskQueue.isEmpty(); }
/** * @see Queue#peek() */ protected Runnable peekTask() { assert inEventLoop(); return taskQueue.peek(); }
private void throwIfInEventLoop(String method) { if (inEventLoop()) { throw new RejectedExecutionException("Calling " + method + " from within the EventLoop is not allowed"); } }
/** * @see Queue#peek() */ protected Runnable peekTask() { assert inEventLoop(); return taskQueue.peek(); }
/** * @see Queue#isEmpty() */ protected boolean hasTasks() { assert inEventLoop(); return !taskQueue.isEmpty(); }
private void throwIfInEventLoop(String method) { if (inEventLoop()) { throw new RejectedExecutionException("Calling " + method + " from within the EventLoop is not allowed"); } }
/** * @see Queue#isEmpty() */ protected boolean hasTasks() { assert inEventLoop(); return !taskQueue.isEmpty(); }
private void throwIfInEventLoop(String method) { if (inEventLoop()) { throw new RejectedExecutionException("Calling " + method + " from within the EventLoop is not allowed"); } }
/** * @see Queue#poll() */ protected Runnable pollTask() { assert inEventLoop(); return pollTaskFrom(taskQueue); }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } if (inEventLoop()) { throw new IllegalStateException("cannot await termination of the current thread"); } if (threadLock.tryAcquire(timeout, unit)) { threadLock.release(); } return isTerminated(); }
/** * Add a {@link Runnable} which will be executed on shutdown of this instance */ public void addShutdownHook(final Runnable task) { if (inEventLoop()) { shutdownHooks.add(task); } else { execute(new Runnable() { @Override public void run() { shutdownHooks.add(task); } }); } }
/** * Remove a previous added {@link Runnable} as a shutdown hook */ public void removeShutdownHook(final Runnable task) { if (inEventLoop()) { shutdownHooks.remove(task); } else { execute(new Runnable() { @Override public void run() { shutdownHooks.remove(task); } }); } }
/** * @see Queue#poll() */ protected Runnable pollTask() { assert inEventLoop(); return pollTaskFrom(taskQueue); }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } if (inEventLoop()) { throw new IllegalStateException("cannot await termination of the current thread"); } if (threadLock.tryAcquire(timeout, unit)) { threadLock.release(); } return isTerminated(); }
/** * Remove a previous added {@link Runnable} as a shutdown hook */ public void removeShutdownHook(final Runnable task) { if (inEventLoop()) { shutdownHooks.remove(task); } else { execute(new Runnable() { @Override public void run() { shutdownHooks.remove(task); } }); } }
/** * Add a {@link Runnable} which will be executed on shutdown of this instance */ public void addShutdownHook(final Runnable task) { if (inEventLoop()) { shutdownHooks.add(task); } else { execute(new Runnable() { @Override public void run() { shutdownHooks.add(task); } }); } }
@Override public void rejected(Runnable task, SingleThreadEventExecutor executor) { if (!executor.inEventLoop()) { for (int i = 0; i < retries; i++) { // Try to wake up the executor so it will empty its task queue. executor.wakeup(false); LockSupport.parkNanos(backOffNanos); if (executor.offerTask(task)) { return; } } } // Either we tried to add the task from within the EventLoop or we was not able to add it even with // backoff. throw new RejectedExecutionException(); } };
@Override public void rejected(Runnable task, SingleThreadEventExecutor executor) { if (!executor.inEventLoop()) { for (int i = 0; i < retries; i++) { // Try to wake up the executor so it will empty its task queue. executor.wakeup(false); LockSupport.parkNanos(backOffNanos); if (executor.offerTask(task)) { return; } } } // Either we tried to add the task from within the EventLoop or we was not able to add it even with // backoff. throw new RejectedExecutionException(); } };
/** * Returns the {@link ThreadProperties} of the {@link Thread} that powers the {@link SingleThreadEventExecutor}. * If the {@link SingleThreadEventExecutor} is not started yet, this operation will start it and block until * it is fully started. */ public final ThreadProperties threadProperties() { ThreadProperties threadProperties = this.threadProperties; if (threadProperties == null) { Thread thread = this.thread; if (thread == null) { assert !inEventLoop(); submit(NOOP_TASK).syncUninterruptibly(); thread = this.thread; assert thread != null; } threadProperties = new DefaultThreadProperties(thread); if (!PROPERTIES_UPDATER.compareAndSet(this, null, threadProperties)) { threadProperties = this.threadProperties; } } return threadProperties; }
/** * Poll all tasks from the task queue and run them via {@link Runnable#run()} method. * * @return {@code true} if and only if at least one task was run */ protected boolean runAllTasks() { assert inEventLoop(); boolean fetchedAll; boolean ranAtLeastOne = false; do { fetchedAll = fetchFromScheduledTaskQueue(); if (runAllTasksFrom(taskQueue)) { ranAtLeastOne = true; } } while (!fetchedAll); // keep on processing until we fetched all scheduled tasks. if (ranAtLeastOne) { lastExecutionTime = ScheduledFutureTask.nanoTime(); } afterRunningAllTasks(); return ranAtLeastOne; }