java.nio.channels.Selector.selectNow()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(294)

本文整理了Java中java.nio.channels.Selector.selectNow()方法的一些代码示例,展示了Selector.selectNow()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Selector.selectNow()方法的具体详情如下:
包路径:java.nio.channels.Selector
类名称:Selector
方法名:selectNow

Selector.selectNow介绍

[英]Detects if any of the registered channels is ready for I/O operations according to its SelectionKey. This operation will return immediately.
[中]根据SelectionKey检测任何已注册的通道是否已准备好进行I/O操作。此操作将立即返回。

代码示例

代码示例来源:origin: netty/netty

int selectNow() throws IOException {
  try {
    return selector.selectNow();
  } finally {
    // restore wakeup state if needed
    if (wakenUp.get()) {
      selector.wakeup();
    }
  }
}

代码示例来源:origin: io.netty/netty

protected final boolean cleanUpCancelledKeys() throws IOException {
  if (cancelledKeys >= CLEANUP_INTERVAL) {
    cancelledKeys = 0;
    selector.selectNow();
    return true;
  }
  return false;
}

代码示例来源:origin: redisson/redisson

int selectNow() throws IOException {
  try {
    return selector.selectNow();
  } finally {
    // restore wakeup state if needed
    if (wakenUp.get()) {
      selector.wakeup();
    }
  }
}

代码示例来源:origin: apache/kafka

/**
 * Check for data, waiting up to the given timeout.
 *
 * @param timeoutMs Length of time to wait, in milliseconds, which must be non-negative
 * @return The number of keys ready
 */
private int select(long timeoutMs) throws IOException {
  if (timeoutMs < 0L)
    throw new IllegalArgumentException("timeout should be >= 0");
  if (timeoutMs == 0L)
    return this.nioSelector.selectNow();
  else
    return this.nioSelector.select(timeoutMs);
}

代码示例来源:origin: wildfly/wildfly

public void run() {
    try {
      selector.selectNow();
    } catch (IOException ignored) {
    }
    done = true;
    unpark(thread);
  }
}

代码示例来源:origin: netty/netty

private void selectAgain() {
    needsToSelectAgain = false;
    try {
      selector.selectNow();
    } catch (Throwable t) {
      logger.warn("Failed to update SelectionKeys.", t);
    }
  }
}

代码示例来源:origin: netty/netty

@Override
public int selectNow() throws IOException {
  selectionKeys.reset();
  return delegate.selectNow();
}

代码示例来源:origin: wildfly/wildfly

int selectNow() throws IOException {
  try {
    return selector.selectNow();
  } finally {
    // restore wakeup state if needed
    if (wakenUp.get()) {
      selector.wakeup();
    }
  }
}

代码示例来源:origin: redisson/redisson

@Override
public int selectNow() throws IOException {
  selectionKeys.reset();
  return delegate.selectNow();
}

代码示例来源:origin: redisson/redisson

private void selectAgain() {
    needsToSelectAgain = false;
    try {
      selector.selectNow();
    } catch (Throwable t) {
      logger.warn("Failed to update SelectionKeys.", t);
    }
  }
}

代码示例来源:origin: wildfly/wildfly

private void selectAgain() {
    needsToSelectAgain = false;
    try {
      selector.selectNow();
    } catch (Throwable t) {
      logger.warn("Failed to update SelectionKeys.", t);
    }
  }
}

代码示例来源:origin: netty/netty

private Selector selectRebuildSelector(int selectCnt) throws IOException {
  // The selector returned prematurely many times in a row.
  // Rebuild the selector to work around the problem.
  logger.warn(
      "Selector.select() returned prematurely {} times in a row; rebuilding Selector {}.",
      selectCnt, selector);
  rebuildSelector();
  Selector selector = this.selector;
  // Select again to populate selectedKeys.
  selector.selectNow();
  return selector;
}

代码示例来源:origin: wildfly/wildfly

@Override
public int selectNow() throws IOException {
  selectionKeys.reset();
  return delegate.selectNow();
}

代码示例来源:origin: EsotericSoftware/kryonet

public void close () {
  super.close();
  synchronized (updateLock) { // Blocks to avoid a select while the selector is used to bind the server connection.
  }
  // Select one last time to complete closing the socket.
  if (!isClosed) {
    isClosed = true;
    selector.wakeup();
    try {
      selector.selectNow();
    } catch (IOException ignored) {
    }
  }
}

代码示例来源:origin: redisson/redisson

private Selector selectRebuildSelector(int selectCnt) throws IOException {
  // The selector returned prematurely many times in a row.
  // Rebuild the selector to work around the problem.
  logger.warn(
      "Selector.select() returned prematurely {} times in a row; rebuilding Selector {}.",
      selectCnt, selector);
  rebuildSelector();
  Selector selector = this.selector;
  // Select again to populate selectedKeys.
  selector.selectNow();
  return selector;
}

代码示例来源:origin: alibaba/nacos

@Override
public void run() {
  while (true) {
    try {
      processTask();
      int readyCount = selector.selectNow();
      if (readyCount <= 0) {
        continue;
      }
      Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
      while (iter.hasNext()) {
        SelectionKey key = iter.next();
        iter.remove();
        NIO_EXECUTOR.execute(new PostProcessor(key));
      }
    } catch (Throwable e) {
      SRV_LOG.error("[HEALTH-CHECK] error while processing NIO task", e);
    }
  }
}

代码示例来源:origin: real-logic/aeron

while (selector.selectNow() == 0)

代码示例来源:origin: real-logic/agrona

/**
   * Explicit call to selectNow but without processing of selected keys.
   */
  public void selectNowWithoutProcessing()
  {
    try
    {
      selector.selectNow();
      selectedKeySet.reset();
    }
    catch (final IOException ex)
    {
      LangUtil.rethrowUnchecked(ex);
    }
  }
}

代码示例来源:origin: netty/netty

if (timeoutMillis <= 0) {
  if (selectCnt == 0) {
    selector.selectNow();
    selectCnt = 1;
  selector.selectNow();
  selectCnt = 1;
  break;

代码示例来源:origin: real-logic/aeron

public int pollTransports()
{
  int bytesReceived = 0;
  try
  {
    if (channelAndTransports.length <= ITERATION_THRESHOLD)
    {
      for (final ChannelAndTransport channelAndTransport : channelAndTransports)
      {
        bytesReceived += poll(channelAndTransport);
      }
    }
    else
    {
      selector.selectNow();
      final SelectionKey[] keys = selectedKeySet.keys();
      for (int i = 0, length = selectedKeySet.size(); i < length; i++)
      {
        bytesReceived += poll((ChannelAndTransport)keys[i].attachment());
      }
      selectedKeySet.reset();
    }
  }
  catch (final IOException ex)
  {
    LangUtil.rethrowUnchecked(ex);
  }
  return bytesReceived;
}

相关文章