欢迎使用CSDN-markdown编辑器

来源:互联网 发布:dijkstra算法例题 编辑:程序博客网 时间:2024/06/05 20:17
 @Override    protected void run() {        for (;;) {            try {                switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {                    case SelectStrategy.CONTINUE:                        continue;                    case SelectStrategy.SELECT:                        select(wakenUp.getAndSet(false));                        // 'wakenUp.compareAndSet(false, true)' is always evaluated                        // before calling 'selector.wakeup()' to reduce the wake-up                        // overhead. (Selector.wakeup() is an expensive operation.)                        //                        // However, there is a race condition in this approach.                        // The race condition is triggered when 'wakenUp' is set to                        // true too early.                        //                        // 'wakenUp' is set to true too early if:                        // 1) Selector is waken up between 'wakenUp.set(false)' and                        //    'selector.select(...)'. (BAD)                        // 2) Selector is waken up between 'selector.select(...)' and                        //    'if (wakenUp.get()) { ... }'. (OK)                        //                        // In the first case, 'wakenUp' is set to true and the                        // following 'selector.select(...)' will wake up immediately.                        // Until 'wakenUp' is set to false again in the next round,                        // 'wakenUp.compareAndSet(false, true)' will fail, and therefore                        // any attempt to wake up the Selector will fail, too, causing                        // the following 'selector.select(...)' call to block                        // unnecessarily.                        //                        // To fix this problem, we wake up the selector again if wakenUp                        // is true immediately after selector.select(...).                        // It is inefficient in that it wakes up the selector for both                        // the first case (BAD - wake-up required) and the second case                        // (OK - no wake-up required).                        if (wakenUp.get()) {                            selector.wakeup();                        }                    default:                        // fallthrough                }                cancelledKeys = 0;                needsToSelectAgain = false;                final int ioRatio = this.ioRatio;                if (ioRatio == 100) {                    processSelectedKeys();                    runAllTasks();                } else {                    final long ioStartTime = System.nanoTime();                    processSelectedKeys();                    final long ioTime = System.nanoTime() - ioStartTime;                    runAllTasks(ioTime * (100 - ioRatio) / ioRatio);                }                if (isShuttingDown()) {                    closeAll();                    if (confirmShutdown()) {                        break;                    }                }            } catch (Throwable t) {                logger.warn("Unexpected exception in the selector loop.", t);                // Prevent possible consecutive immediate failures that lead to                // excessive CPU consumption.                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    // Ignore.                }            }        }    }
原创粉丝点击