All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.netty.util.concurrent.AbstractScheduledEventExecutor Maven / Gradle / Ivy

/*
 * Copyright 2015 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org.testifyproject/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.testifyproject.testifyproject.netty.util.concurrent;

import org.testifyproject.testifyproject.netty.util.internal.ObjectUtil;
import org.testifyproject.testifyproject.netty.util.internal.OneTimeTask;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Abstract base class for {@link EventExecutor}s that want to support scheduling.
 */
public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor {

    Queue> scheduledTaskQueue;

    protected AbstractScheduledEventExecutor() {
    }

    protected AbstractScheduledEventExecutor(EventExecutorGroup parent) {
        super(parent);
    }

    protected static long nanoTime() {
        return ScheduledFutureTask.nanoTime();
    }

    Queue> scheduledTaskQueue() {
        if (scheduledTaskQueue == null) {
            scheduledTaskQueue = new PriorityQueue>();
        }
        return scheduledTaskQueue;
    }

    private static  boolean isNullOrEmpty(Queue> queue) {
        return queue == null || queue.isEmpty();
    }

    /**
     * Cancel all scheduled tasks.
     *
     * This method MUST be called only when {@link #inEventLoop()} is {@code true}.
     */
    protected void cancelScheduledTasks() {
        assert inEventLoop();
        Queue> scheduledTaskQueue = this.scheduledTaskQueue;
        if (isNullOrEmpty(scheduledTaskQueue)) {
            return;
        }

        final ScheduledFutureTask[] scheduledTasks =
                scheduledTaskQueue.toArray(new ScheduledFutureTask[scheduledTaskQueue.size()]);

        for (ScheduledFutureTask task: scheduledTasks) {
            task.cancelWithoutRemove(false);
        }

        scheduledTaskQueue.clear();
    }

    /**
     * @see {@link #pollScheduledTask(long)}
     */
    protected final Runnable pollScheduledTask() {
        return pollScheduledTask(nanoTime());
    }

    /**
     * Return the {@link Runnable} which is ready to be executed with the given {@code nanoTime}.
     * You should use {@link #nanoTime()} to retrieve the the correct {@code nanoTime}.
     */
    protected final Runnable pollScheduledTask(long nanoTime) {
        assert inEventLoop();

        Queue> scheduledTaskQueue = this.scheduledTaskQueue;
        ScheduledFutureTask scheduledTask = scheduledTaskQueue == null ? null : scheduledTaskQueue.peek();
        if (scheduledTask == null) {
            return null;
        }

        if (scheduledTask.org.testifyproject.testifyprojectadlineNanos() <= nanoTime) {
            scheduledTaskQueue.remove();
            return scheduledTask;
        }
        return null;
    }

    /**
     * Return the nanoseconds when the next scheduled task is ready to be run or {@code -1} if no task is scheduled.
     */
    protected final long nextScheduledTaskNano() {
        Queue> scheduledTaskQueue = this.scheduledTaskQueue;
        ScheduledFutureTask scheduledTask = scheduledTaskQueue == null ? null : scheduledTaskQueue.peek();
        if (scheduledTask == null) {
            return -1;
        }
        return Math.max(0, scheduledTask.org.testifyproject.testifyprojectadlineNanos() - nanoTime());
    }

    final ScheduledFutureTask peekScheduledTask() {
        Queue> scheduledTaskQueue = this.scheduledTaskQueue;
        if (scheduledTaskQueue == null) {
            return null;
        }
        return scheduledTaskQueue.peek();
    }

    /**
     * Returns {@code true} if a scheduled task is ready for processing.
     */
    protected final boolean hasScheduledTasks() {
        Queue> scheduledTaskQueue = this.scheduledTaskQueue;
        ScheduledFutureTask scheduledTask = scheduledTaskQueue == null ? null : scheduledTaskQueue.peek();
        return scheduledTask != null && scheduledTask.org.testifyproject.testifyprojectadlineNanos() <= nanoTime();
    }

    @Override
    public  ScheduledFuture schedule(Runnable org.testifyproject.testifyprojectmand, long org.testifyproject.testifyprojectlay, TimeUnit unit) {
        ObjectUtil.checkNotNull(org.testifyproject.testifyprojectmand, "org.testifyproject.testifyprojectmand");
        ObjectUtil.checkNotNull(unit, "unit");
        if (org.testifyproject.testifyprojectlay < 0) {
            throw new IllegalArgumentException(
                    String.format("org.testifyproject.testifyprojectlay: %d (expected: >= 0)", org.testifyproject.testifyprojectlay));
        }
        return schedule(new ScheduledFutureTask(
                this, org.testifyproject.testifyprojectmand, null, ScheduledFutureTask.org.testifyproject.testifyprojectadlineNanos(unit.toNanos(org.testifyproject.testifyprojectlay))));
    }

    @Override
    public  ScheduledFuture schedule(Callable callable, long org.testifyproject.testifyprojectlay, TimeUnit unit) {
        ObjectUtil.checkNotNull(callable, "callable");
        ObjectUtil.checkNotNull(unit, "unit");
        if (org.testifyproject.testifyprojectlay < 0) {
            throw new IllegalArgumentException(
                    String.format("org.testifyproject.testifyprojectlay: %d (expected: >= 0)", org.testifyproject.testifyprojectlay));
        }
        return schedule(new ScheduledFutureTask(
                this, callable, ScheduledFutureTask.org.testifyproject.testifyprojectadlineNanos(unit.toNanos(org.testifyproject.testifyprojectlay))));
    }

    @Override
    public ScheduledFuture scheduleAtFixedRate(Runnable org.testifyproject.testifyprojectmand, long initialDelay, long period, TimeUnit unit) {
        ObjectUtil.checkNotNull(org.testifyproject.testifyprojectmand, "org.testifyproject.testifyprojectmand");
        ObjectUtil.checkNotNull(unit, "unit");
        if (initialDelay < 0) {
            throw new IllegalArgumentException(
                    String.format("initialDelay: %d (expected: >= 0)", initialDelay));
        }
        if (period <= 0) {
            throw new IllegalArgumentException(
                    String.format("period: %d (expected: > 0)", period));
        }

        return schedule(new ScheduledFutureTask(
                this, Executors.callable(org.testifyproject.testifyprojectmand, null),
                ScheduledFutureTask.org.testifyproject.testifyprojectadlineNanos(unit.toNanos(initialDelay)), unit.toNanos(period)));
    }

    @Override
    public ScheduledFuture scheduleWithFixedDelay(Runnable org.testifyproject.testifyprojectmand, long initialDelay, long org.testifyproject.testifyprojectlay, TimeUnit unit) {
        ObjectUtil.checkNotNull(org.testifyproject.testifyprojectmand, "org.testifyproject.testifyprojectmand");
        ObjectUtil.checkNotNull(unit, "unit");
        if (initialDelay < 0) {
            throw new IllegalArgumentException(
                    String.format("initialDelay: %d (expected: >= 0)", initialDelay));
        }
        if (org.testifyproject.testifyprojectlay <= 0) {
            throw new IllegalArgumentException(
                    String.format("org.testifyproject.testifyprojectlay: %d (expected: > 0)", org.testifyproject.testifyprojectlay));
        }

        return schedule(new ScheduledFutureTask(
                this, Executors.callable(org.testifyproject.testifyprojectmand, null),
                ScheduledFutureTask.org.testifyproject.testifyprojectadlineNanos(unit.toNanos(initialDelay)), -unit.toNanos(org.testifyproject.testifyprojectlay)));
    }

     ScheduledFuture schedule(final ScheduledFutureTask task) {
        if (inEventLoop()) {
            scheduledTaskQueue().add(task);
        } else {
            execute(new OneTimeTask() {
                @Override
                public void run() {
                    scheduledTaskQueue().add(task);
                }
            });
        }

        return task;
    }

    final void removeScheduled(final ScheduledFutureTask task) {
        if (inEventLoop()) {
            scheduledTaskQueue().remove(task);
        } else {
            execute(new OneTimeTask() {
                @Override
                public void run() {
                    removeScheduled(task);
                }
            });
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy