Repository: netty/netty
File: transport/src/main/java/io/netty/channel/ManualIoEventLoop.java (Java)

Code before:
    public ManualIoEventLoop(IoEventLoopGroup parent, Thread owningThread, IoHandlerFactory factory, Ticker ticker) {
        this.parent = parent;
        this.owningThread = new AtomicReference<>(owningThread);
        this.handler = factory.newHandler(this);
        this.ticker = Objects.requireNonNull(ticker, "ticker");
        state = new AtomicInteger(ST_STARTED);
    }

    @Override
    public Ticker ticker() {
        return ticker;
    }

    /**
     * Poll all tasks from the task queue and run them via {@link Runnable#run()} method.
     * <br>
     * The behaviour of this method is the same as {@link #runNonIoTasks(long)}
     * with {@code timeoutNanos} set {@code <= 0}.
     */
    public int runNonIoTasks() {
        return runAllTasks(-1, true);
    }

    /**
     * Poll all tasks from the task queue and run them via {@link Runnable#run()} method.  This method stops running
     * the tasks in the task queue and returns if it ran longer than {@code timeoutNanos}.<br>
     *
     * @param timeoutNanos the maximum time in nanoseconds to run tasks. If {@code <= 0}, no timeout is applied.
     */
    public int runNonIoTasks(long timeoutNanos) {
        return runAllTasks(timeoutNanos, true);
    }

    private int runAllTasks(long timeoutNanos, boolean setCurrentExecutor) {
        assert inEventLoop();
        final Queue<Runnable> taskQueue = this.taskQueue;
        // since the taskQueue is unbounded we don't need to keep on calling this while draining it.
        boolean alwaysTrue = fetchFromScheduledTaskQueue(taskQueue);
        assert alwaysTrue;
        Runnable task = taskQueue.poll();
        if (task == null) {
            return 0;
        }
        EventExecutor old = setCurrentExecutor? ThreadExecutorMap.setCurrentExecutor(this) : null;
        try {
            final long deadline = timeoutNanos > 0 ? getCurrentTimeNanos() + timeoutNanos : 0;
            int runTasks = 0;
            long lastExecutionTime;
            final Ticker ticker = this.ticker;
            for (;;) {
                safeExecute(task);

Review comment (suggestion):
We could rephrase the javadoc like "Poll and run tasks from the task queue, until the task queue is empty or the given deadline (if positive) is exceeded."

I think that'd be clearer.

Code after:
        this.parent = parent;
        this.owningThread = new AtomicReference<>(owningThread);
        this.handler = factory.newHandler(this);
        this.ticker = Objects.requireNonNull(ticker, "ticker");
        state = new AtomicInteger(ST_STARTED);
    }

    @Override
    public Ticker ticker() {
        return ticker;
    }

    /**
     * Poll and run tasks from the task queue, until the task queue is empty or the given deadline is exceeded.<br>
     * If {@code timeoutNanos} is less or equals 0, no deadline is applied.
     *
     * @param timeoutNanos the maximum time in nanoseconds to run tasks.
     */
    public int runNonBlockingTasks(long timeoutNanos) {
        return runAllTasks(timeoutNanos, true);
    }

    private int runAllTasks(long timeoutNanos, boolean setCurrentExecutor) {
        assert inEventLoop();
        final Queue<Runnable> taskQueue = this.taskQueue;
        // since the taskQueue is unbounded we don't need to keep on calling this while draining it.
        boolean alwaysTrue = fetchFromScheduledTaskQueue(taskQueue);
        assert alwaysTrue;
        Runnable task = taskQueue.poll();
        if (task == null) {
            return 0;
        }
        EventExecutor old = setCurrentExecutor? ThreadExecutorMap.setCurrentExecutor(this) : null;
        try {
            final long deadline = timeoutNanos > 0 ? getCurrentTimeNanos() + timeoutNanos : 0;
            int runTasks = 0;
            long lastExecutionTime;
            final Ticker ticker = this.ticker;
            for (;;) {
                safeExecute(task);

                runTasks++;

               if (timeoutNanos > 0) {
                    lastExecutionTime = ticker.nanoTime();
                    if ((lastExecutionTime - deadline) >= 0) {
                        break;
                    }
                }

                task = taskQueue.poll();