本文整理汇总了Java中java.nio.channels.Selector.selectedKeys方法的典型用法代码示例。如果您正苦于以下问题:Java Selector.selectedKeys方法的具体用法?Java Selector.selectedKeys怎么用?Java Selector.selectedKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.Selector
的用法示例。
在下文中一共展示了Selector.selectedKeys方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
//轮询发现新连接请求
for (;;) {
++acceptCount;
try {
// 阻塞的,当超时,有注册的响应事件,或者被执行wakeup方法时继续
tSelector.select(1000L);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.warn(getName(), e);
}
}
}
示例2: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector selector = this.selector;
for (;;) {
++acceptCount;
try {
selector.select( 1000L );
Set<SelectionKey> keys = selector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Throwable e) {
LOGGER.warn(getName(), e);
}
}
}
示例3: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
for (; ; ) {
try {
tSelector.select(1000L);
connect(tSelector);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
Object att = key.attachment();
if (att != null && key.isValid() && key.isConnectable()) {
finishConnect(key, att);
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.info(name, e);
}
}
}
示例4: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
for (; ; ) {
try {
tSelector.select(1000L);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.info(getName(), e);
}
}
}
示例5: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
for (;;) {
++connectCount;
try {
// 查看有无连接就绪 阻塞的
tSelector.select(1000L);
connect(tSelector);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
Object att = key.attachment();
if (att != null && key.isValid() && key.isConnectable()) {
finishConnect(key, att);
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.warn(name, e);
}
}
}
示例6: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
public void run() throws IOException {
Selector selector = Selector.open();
// will register the socket on the selector
TunnelServer tunnelServer = new TunnelServer(port, selector);
Log.i(TAG, "Relay server started");
long nextCleaningDeadline = System.currentTimeMillis() + UDPConnection.IDLE_TIMEOUT;
while (true) {
long timeout = Math.max(0, nextCleaningDeadline - System.currentTimeMillis());
selector.select(timeout);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
long now = System.currentTimeMillis();
if (now >= nextCleaningDeadline) {
tunnelServer.cleanUp();
nextCleaningDeadline = now + CLEANING_INTERVAL;
} else if (selectedKeys.isEmpty()) {
throw new AssertionError("selector.select() returned without any event, an invalid SelectionKey was probably been registered");
}
for (SelectionKey selectedKey : selectedKeys) {
SelectionHandler selectionHandler = (SelectionHandler) selectedKey.attachment();
selectionHandler.onReady(selectedKey);
}
// by design, we handled everything
selectedKeys.clear();
}
}
示例7: run
import java.nio.channels.Selector; //导入方法依赖的package包/类
@Override
public void run() {
final Selector selector = this.selector;
for (;;) {
++connectCount;
try {
//查看有无连接就绪
selector.select( 1000L );
connect(selector);
Set<SelectionKey> keys = selector.selectedKeys();
try {
for (SelectionKey key: keys) {
Object att = key.attachment();
if (att != null && key.isValid() && key.isConnectable()) {
finishConnect(key, att);
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.warn(name, e);
}
}
}