本文整理匯總了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);
}
}
}