當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。