Search in sources :

Example 1 with DelayedQueue

use of com.firenio.collection.DelayedQueue in project baseio by generallycloud.

the class NioEventLoop method run.

@Override
public void run() {
    // does it useful to set variables locally ?
    final long idle = group.getIdleTime();
    final AtomicInteger selecting = this.selecting;
    final BlockingQueue<Runnable> events = this.events;
    final DelayedQueue dq = this.delayed_queue;
    long last_idle_time = Util.now();
    long next_idle_time = last_idle_time + idle;
    long select_time = idle;
    for (; ; ) {
        // restart will create a new event loop instead.
        if (!isRunning()) {
            shutdown();
            return;
        }
        try {
            // the method selector.wakeup is a weight operator, so we use flag "has_task"
            // and race flag "selecting" to reduce execution times of wake up
            // I am not sure events.size if a better way to instead of has_task?
            // example method selector.select(...) may throw an io exception
            // and if we need to try with the method to do something when exception caught?
            int selected;
            if (!has_task() && selecting.compareAndSet(0, 1)) {
                if (has_task()) {
                    selected = select_now();
                } else {
                    selected = select(select_time);
                }
                selecting.set(0);
            } else {
                selected = select_now();
            }
            clear_has_task();
            if (selected > 0) {
                accept(selected);
            }
            long now = Util.now();
            if (now >= next_idle_time) {
                channel_idle(last_idle_time);
                last_idle_time = next_idle_time;
                next_idle_time = next_idle_time + idle;
            }
            run_events(events);
            if (!dq.isEmpty()) {
                select_time = run_delayed_events(dq, next_idle_time);
            } else {
                select_time = next_idle_time - Util.now();
            }
            remove_closed_channels();
        } catch (Throwable e) {
            logger.error(e);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DelayedQueue(com.firenio.collection.DelayedQueue)

Example 2 with DelayedQueue

use of com.firenio.collection.DelayedQueue in project baseio by generallycloud.

the class TestDelayedWorkQueue method main.

public static void main(String[] args) {
    DelayedQueue q = new DelayedQueue();
    q.offer(new TestDelayTask(10));
    q.offer(new TestDelayTask(5));
    q.offer(new TestDelayTask(15));
    q.offer(new TestDelayTask(1));
    q.offer(new TestDelayTask(2));
    q.offer(new TestDelayTask(18));
    q.offer(new TestDelayTask(12));
    q.offer(new TestDelayTask(2));
    for (; ; ) {
        DelayTask t = q.poll();
        if (t == null) {
            break;
        }
        t.run();
    }
}
Also used : DelayTask(com.firenio.collection.DelayedQueue.DelayTask) DelayedQueue(com.firenio.collection.DelayedQueue)

Aggregations

DelayedQueue (com.firenio.collection.DelayedQueue)2 DelayTask (com.firenio.collection.DelayedQueue.DelayTask)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1