当前位置: 首页>>代码示例>>Java>>正文


Java Selector.selectedKeys方法代码示例

本文整理汇总了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);
		}
	}
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:27,代码来源:NIOAcceptor.java

示例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);
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:25,代码来源:NIOAcceptor.java

示例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);
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:26,代码来源:NIOConnector.java

示例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);
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:24,代码来源:NIOAcceptor.java

示例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);
		}
	}
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:28,代码来源:NIOConnector.java

示例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();
    }
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:31,代码来源:Relay.java

示例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);
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:28,代码来源:NIOConnector.java


注:本文中的java.nio.channels.Selector.selectedKeys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。