2015-10-22 20:02:55 +02:00
|
|
|
package com.jenkov.nioserver;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.nio.channels.SelectionKey;
|
|
|
|
import java.nio.channels.Selector;
|
|
|
|
import java.nio.channels.SocketChannel;
|
|
|
|
|
2019-12-28 10:43:48 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
2015-10-22 20:02:55 +02:00
|
|
|
/**
|
2019-12-28 10:43:48 +01:00
|
|
|
* Project: <strong>java-nio-server</strong><br>
|
|
|
|
* File: <strong>SelectorTest.java</strong><br>
|
|
|
|
* Created: <strong>21 Oct 2015</strong><br>
|
|
|
|
*
|
|
|
|
* @author jjenkov
|
2015-10-22 20:02:55 +02:00
|
|
|
*/
|
|
|
|
public class SelectorTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test() throws IOException {
|
|
|
|
Selector selector = Selector.open();
|
|
|
|
|
|
|
|
SocketChannel socketChannel = SocketChannel.open();
|
|
|
|
socketChannel.bind(new InetSocketAddress("localhost", 9999));
|
|
|
|
|
|
|
|
socketChannel.configureBlocking(false);
|
|
|
|
|
|
|
|
SelectionKey key1 = socketChannel.register(selector, SelectionKey.OP_WRITE);
|
|
|
|
key1.cancel();
|
|
|
|
|
|
|
|
SelectionKey key2 = socketChannel.register(selector, SelectionKey.OP_WRITE);
|
|
|
|
key2.cancel();
|
|
|
|
}
|
2019-12-28 10:43:48 +01:00
|
|
|
}
|