java 8 aio_高并发Java(8):NIO和AIO(下)

package test;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.nio.ByteBuffer;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.nio.channels.spi.AbstractSelector;

import java.nio.channels.spi.SelectorProvider;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class MultiThreadNIOEchoServer {

public static Map geym_time_stat = new HashMap();

class EchoClient {

private LinkedList outq;

EchoClient() {

outq = new LinkedList();

}

public LinkedList getOutputQueue() {

return outq;

}

public void enqueue(ByteBuffer bb) {

outq.addFirst(bb);

}

}

class HandleMsg implements Runnable {

SelectionKey sk;

ByteBuffer bb;

public HandleMsg(SelectionKey sk, ByteBuffer bb) {

super();

this.sk = sk;

this.bb = bb;

}

@Override

public void run() {

// TODO Auto-generated method stub

EchoClient echoClient = (EchoClient) sk.attachment();

echoClient.enqueue(bb);

sk.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);

selector.wakeup();

}

}

private Selector selector;

private ExecutorService tp = Executors.newCachedThreadPool();

private void startServer() throws Exception {

selector = SelectorProvider.provider().openSelector();

ServerSocketChannel ssc = ServerSocketChannel.open();

ssc.configureBlocking(false);

InetSocketAddress isa = new InetSocketAddress(8000);

ssc.socket().bind(isa);

// 注册感兴趣的事件,此处对accpet事件感兴趣

SelectionKey acceptKey = ssc.register(selector, SelectionKey.OP_ACCEPT);

for (;;) {

selector.select();

Set readyKeys = selector.selectedKeys();

Iterator i = readyKeys.iterator();

long e = 0;

while (i.hasNext()) {

SelectionKey sk = (SelectionKey) i.next();

i.remove();

if (sk.isAcceptable()) {

doAccept(sk);

} else if (sk.isValid() && sk.isReadable()) {

if (!geym_time_stat.containsKey(((SocketChannel) sk

.channel()).socket())) {

geym_time_stat.put(

((SocketChannel) sk.channel()).socket(),

System.currentTimeMillis());

}

doRead(sk);

} else if (sk.isValid() && sk.isWritable()) {

doWrite(sk);

e = System.currentTimeMillis();

long b = geym_time_stat.remove(((SocketChannel) sk

.channel()).socket());

System.out.println("spend:" + (e - b) + "ms");

}

}

}

}

private void doWrite(SelectionKey sk) {

// TODO Auto-generated method stub

SocketChannel channel = (SocketChannel) sk.channel();

EchoClient echoClient = (EchoClient) sk.attachment();

LinkedList outq = echoClient.getOutputQueue();

ByteBuffer bb = outq.getLast();

try {

int len = channel.write(bb);

if (len == -1) {

disconnect(sk);

return;

}

if (bb.remaining() == 0) {

outq.removeLast();

}

} catch (Exception e) {

// TODO: handle exception

disconnect(sk);

}

if (outq.size() == 0) {

sk.interestOps(SelectionKey.OP_READ);

}

}

private void doRead(SelectionKey sk) {

// TODO Auto-generated method stub

SocketChannel channel = (SocketChannel) sk.channel();

ByteBuffer bb = ByteBuffer.allocate(8192);

int len;

try {

len = channel.read(bb);

if (len < 0) {

disconnect(sk);

return;

}

} catch (Exception e) {

// TODO: handle exception

disconnect(sk);

return;

}

bb.flip();

tp.execute(new HandleMsg(sk, bb));

}

private void disconnect(SelectionKey sk) {

// TODO Auto-generated method stub

//省略略干关闭操作

}

private void doAccept(SelectionKey sk) {

// TODO Auto-generated method stub

ServerSocketChannel server = (ServerSocketChannel) sk.channel();

SocketChannel clientChannel;

try {

clientChannel = server.accept();

clientChannel.configureBlocking(false);

SelectionKey clientKey = clientChannel.register(selector,

SelectionKey.OP_READ);

EchoClient echoClinet = new EchoClient();

clientKey.attach(echoClinet);

InetAddress clientAddress = clientChannel.socket().getInetAddress();

System.out.println("Accepted connection from "

+ clientAddress.getHostAddress());

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

MultiThreadNIOEchoServer echoServer = new MultiThreadNIOEchoServer();

try {

echoServer.startServer();

} catch (Exception e) {

// TODO: handle exception

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值