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


Java Selector.selectNow方法代码示例

本文整理汇总了Java中java.nio.channels.Selector.selectNow方法的典型用法代码示例。如果您正苦于以下问题:Java Selector.selectNow方法的具体用法?Java Selector.selectNow怎么用?Java Selector.selectNow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.Selector的用法示例。


在下文中一共展示了Selector.selectNow方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.nio.channels.Selector; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
    Selector sel = Selector.open();
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    SelectionKey key = sc.register(sel, 0);
    for (int i=0; i<50000; i++) {
        key.interestOps(0);
    }
    sel.selectNow();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:LotsOfUpdates.java

示例2: write

import java.nio.channels.Selector; //导入方法依赖的package包/类
public int write(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout, boolean block)
		throws IOException {
	if (SHARED && block) {
		return blockingSelector.write(buf, socket, writeTimeout);
	}
	SelectionKey key = null;
	int written = 0;
	boolean timedout = false;
	int keycount = 1; // assume we can write
	long time = System.currentTimeMillis(); // start the timeout timer
	try {
		while ((!timedout) && buf.hasRemaining()) {
			int cnt = 0;
			if (keycount > 0) { // only write if we were registered for a
								// write
				cnt = socket.write(buf); // write the data
				if (cnt == -1)
					throw new EOFException();

				written += cnt;
				if (cnt > 0) {
					time = System.currentTimeMillis(); // reset our timeout
														// timer
					continue; // we successfully wrote, try again without a
								// selector
				}
				if (cnt == 0 && (!block))
					break; // don't block
			}
			if (selector != null) {
				// register OP_WRITE to the selector
				if (key == null)
					key = socket.getIOChannel().register(selector, SelectionKey.OP_WRITE);
				else
					key.interestOps(SelectionKey.OP_WRITE);
				keycount = selector.select(writeTimeout);
			}
			if (writeTimeout > 0 && (selector == null || keycount == 0))
				timedout = (System.currentTimeMillis() - time) >= writeTimeout;
		} // while
		if (timedout)
			throw new SocketTimeoutException();
	} finally {
		if (key != null) {
			key.cancel();
			if (selector != null)
				selector.selectNow();// removes the key from this selector
		}
	}
	return written;
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:52,代码来源:NioSelectorPool.java

示例3: select

import java.nio.channels.Selector; //导入方法依赖的package包/类
private void select() throws IOException {
    Selector selector = this.selector;
    try {
        int selectCnt = 0;
        long currentNanoTime = System.nanoTime();
        long selectDeadLineNanos = currentNanoTime + TimeUnit.SECONDS.toNanos(1);
        for (;;) {
            long timeoutMillis = (selectDeadLineNanos - currentNanoTime + 500000L) / 1000000L;
            if (timeoutMillis <= 0) {
                if (selectCnt == 0) {
                    selector.selectNow();
                    selectCnt = 1;
                }
                break;
            }

            int selectedKeys = selector.select(timeoutMillis);
            selectCnt++;

            if (selectedKeys != 0) {
                break;
            }

            if (selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
                LOGGER.warn("Selector.select() returned prematurely {} times in a row; rebuilding selector.", selectCnt);

                rebuildSelector();
                selector = this.selector;

                // 重新select,填充 selectedKeys
                selector.selectNow();
                selectCnt = 1;
                break;
            }

            currentNanoTime = System.nanoTime();
        }

        if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Selector.select() returned prematurely {} times in a row.", selectCnt - 1);
            }
        }
    } catch (CancelledKeyException e) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector - JDK bug?", e);
        }
    }
}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:50,代码来源:NioSelectorLoop.java

示例4: write

import java.nio.channels.Selector; //导入方法依赖的package包/类
public int write(ByteBuffer buf, NioChannel socket, Selector selector,
                 long writeTimeout, boolean block) throws IOException {
    if ( SHARED && block ) {
        return blockingSelector.write(buf,socket,writeTimeout);
    }
    SelectionKey key = null;
    int written = 0;
    boolean timedout = false;
    int keycount = 1; //assume we can write
    long time = System.currentTimeMillis(); //start the timeout timer
    try {
        while ( (!timedout) && buf.hasRemaining() ) {
            int cnt = 0;
            if ( keycount > 0 ) { //only write if we were registered for a write
                cnt = socket.write(buf); //write the data
                if (cnt == -1) throw new EOFException();

                written += cnt;
                if (cnt > 0) {
                    time = System.currentTimeMillis(); //reset our timeout timer
                    continue; //we successfully wrote, try again without a selector
                }
                if (cnt==0 && (!block)) break; //don't block
            }
            if ( selector != null ) {
                //register OP_WRITE to the selector
                if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_WRITE);
                else key.interestOps(SelectionKey.OP_WRITE);
                keycount = selector.select(writeTimeout);
            }
            if (writeTimeout > 0 && (selector == null || keycount == 0) ) timedout = (System.currentTimeMillis()-time)>=writeTimeout;
        }//while
        if ( timedout ) throw new SocketTimeoutException();
    } finally {
        if (key != null) {
            key.cancel();
            if (selector != null) selector.selectNow();//removes the key from this selector
        }
    }
    return written;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:42,代码来源:NioSelectorPool.java

示例5: runTest

import java.nio.channels.Selector; //导入方法依赖的package包/类
static void runTest(int initCount, int massCount, int maxSelectTime)
        throws Exception {
    testStartTime = System.nanoTime();

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 7359);

    // Create server channel, add it to selector and run epoll_ctl.
    log("Setting up server");
    Selector serverSelector = Selector.open();
    ServerSocketChannel server = ServerSocketChannel.open();
    server.configureBlocking(false);
    server.socket().bind(address, 5000);
    server.register(serverSelector, SelectionKey.OP_ACCEPT);
    serverSelector.selectNow();

    log("Setting up client");
    ClientThread client = new ClientThread(address);
    client.start();
    Thread.sleep(100);

    // Set up initial set of client sockets.
    log("Starting initial client connections");
    client.connectClients(initCount);
    Thread.sleep(500);  // Wait for client connections to arrive

    // Accept all initial client sockets, add to selector and run
    // epoll_ctl.
    log("Accepting initial connections");
    List<SocketChannel> serverChannels1 =
        acceptAndAddAll(serverSelector, server, initCount);
    if (serverChannels1.size() != initCount) {
        throw new Exception("Accepted " + serverChannels1.size() +
                            " instead of " + initCount);
    }
    serverSelector.selectNow();

    // Set up mass set of client sockets.
    log("Requesting mass client connections");
    client.connectClients(massCount);
    Thread.sleep(500);  // Wait for client connections to arrive

    // Accept all mass client sockets, add to selector and do NOT
    // run epoll_ctl.
    log("Accepting mass connections");
    List<SocketChannel> serverChannels2 =
        acceptAndAddAll(serverSelector, server, massCount);
    if (serverChannels2.size() != massCount) {
        throw new Exception("Accepted " + serverChannels2.size() +
                            " instead of " + massCount);
    }

    // Close initial set of sockets.
    log("Closing initial connections");
    closeAll(serverChannels1);

    // Now get the timing of select() call.
    log("Running the final select call");
    long startTime = System.nanoTime();
    serverSelector.selectNow();
    long duration = durationMillis(startTime);
    log("Init count = " + initCount +
        ", mass count = " + massCount +
        ", duration = " + duration + "ms");

    if (duration > maxSelectTime) {
        System.out.println
            ("\n\n\n\n\nFAILURE: The final selectNow() took " +
             duration + "ms " +
             "- seems like O(N^2) bug is still here\n\n");
        System.exit(1);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:73,代码来源:LotsOfCancels.java


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