java NIO多路复用简单实例

To set up a Selector to monitor three Socket channels, you'd do something like this

Selector selector = Selector.open();
channel1.register (selector, SelectionKey.OP_READ);
channel2.register (selector, SelectionKey.OP_WRITE);
channel3.register (selector, SelectionKey.OP_READ |
SelectionKey.OP_WRITE);
// Wait up to 10 seconds for a channel to become ready
readyCount = selector.select (10000);



package com.ronsoft.books.nio.channels;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.nio.channels.SelectableChannel;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetSocketAddress;
import java.util.Iterator;
/**
* Simple echo-back server which listens for incoming stream connections
* and echoes back whatever it reads. A single Selector object is used to
* listen to the server socket (to accept new connections) and all the
* active socket channels.
*
* @author Ron Hitchens (ron@ronsoft.com)
*/
public class SelectSockets
{
public static int PORT_NUMBER = 1234;
public static void main (String [] argv)
throws Exception
{
new SelectSockets().go (argv);
}
public void go (String [] argv)
throws Exception
{
int port = PORT_NUMBER;
if (argv.length > 0) { // Override default listen port
port = Integer.parseInt (argv [0]);
}
System.out.println ("Listening on port " + port);
// Allocate an unbound server socket channel
ServerSocketChannel serverChannel = ServerSocketChannel.open();
// Get the associated ServerSocket to bind it with
ServerSocket serverSocket = serverChannel.socket();
// Create a new Selector for use below
Selector selector = Selector.open();
// Set the port the server channel will listen to
serverSocket.bind (new InetSocketAddress (port));
// Set nonblocking mode for the listening socket
serverChannel.configureBlocking (false);
// Register the ServerSocketChannel with the Selector
serverChannel.register (selector, SelectionKey.OP_ACCEPT);
while (true) {
// This may block for a long time. Upon returning, the
// selected set contains keys of the ready channels.
int n = selector.select();
if (n == 0) {
continue; // nothing to do
}
// Get an iterator over the set of selected keys
Iterator it = selector.selectedKeys().iterator();
// Look at each key in the selected set
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
// Is a new connection coming in?
if (key.isAcceptable()) {
ServerSocketChannel server =
(ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();
registerChannel (selector, channel,
SelectionKey.OP_READ);
sayHello (channel);
}
// Is there data to read on this channel?
if (key.isReadable()) {
readDataFromSocket (key);
}
// Remove key from selected set; it's been handled
it.remove();
}
}
}
// ----------------------------------------------------------

/**
* Register the given channel with the given selector for
* the given operations of interest
*/
protected void registerChannel (Selector selector,
SelectableChannel channel, int ops)
throws Exception
{
if (channel == null) {
return; // could happen
}
// Set the new channel nonblocking
channel.configureBlocking (false);
// Register it with the selector
channel.register (selector, ops);
}

以上只是部分代码


readDataFromSocket (key);

的内容没有列出,大家可以参考Oreilly JAVA NIO一书
本书作者给出了两种模型
始终使用一个线程处理数据,还有一种就是使用线程池工作者模式,在多核环境下使用多线程可以更大程度的提供CPU利用率。

下篇简单介绍服务器的3种并发模型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值