异步io java_Java AIO 异步IO应用实例

packageorg.windwant.io.aio;importjava.io.IOException;importjava.net.InetSocketAddress;importjava.nio.ByteBuffer;importjava.nio.channels.AsynchronousChannelGroup;importjava.nio.channels.AsynchronousServerSocketChannel;importjava.nio.channels.AsynchronousSocketChannel;importjava.nio.channels.CompletionHandler;importjava.nio.charset.Charset;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.Executors;importjava.util.concurrent.ThreadLocalRandom;/*** AsynchronousServerSocketChannel*/

public class AIOServer implementsRunnable{private int port = 8889;private int threadSize = 10;protectedAsynchronousChannelGroup asynchronousChannelGroup;protectedAsynchronousServerSocketChannel serverChannel;public AIOServer(int port, intthreadSize) {this.port =port;this.threadSize =threadSize;

init();

}private voidinit(){try{

asynchronousChannelGroup= AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10);

serverChannel=AsynchronousServerSocketChannel.open(asynchronousChannelGroup);

serverChannel.bind(newInetSocketAddress(port));

System.out.println("listening on port: " +port);

}catch(IOException e) {

e.printStackTrace();

}

}public voidrun() {try{if(serverChannel == null) return;

serverChannel.accept(this, new CompletionHandler() {final ByteBuffer echoBuffer = ByteBuffer.allocateDirect(1024);public voidcompleted(AsynchronousSocketChannel result, AIOServer attachment) {

System.out.println("==============================================================");

System.out.println("server process begin ...");try{

System.out.println("client host: " +result.getRemoteAddress());

echoBuffer.clear();

result.read(echoBuffer).get();

echoBuffer.flip();

System.out.println("received : " +Charset.defaultCharset().decode(echoBuffer));int random = ThreadLocalRandom.current().nextInt(5);

printProcess(random);

System.out.println("server deal request execute: " + random + "s");

String msg= "server test msg-" +Math.random();

System.out.println("server send data: " +msg);

result.write(ByteBuffer.wrap(msg.getBytes()));

System.out.println("server process end ...");

}catch(IOException e) {

e.printStackTrace();

}catch(InterruptedException e) {

e.printStackTrace();

}catch(ExecutionException e) {

e.printStackTrace();

}finally{

attachment.serverChannel.accept(attachment,this);//监听新的请求,递归调用。

}

}public voidfailed(Throwable exc, AIOServer attachment) {

System.out.println("received failed");

exc.printStackTrace();

attachment.serverChannel.accept(attachment,this);

}

});

System.in.read();

}catch(Exception e){

e.printStackTrace();

}

}private void printProcess(int s) throwsInterruptedException {

String dot= "";for (int i = 0; i < s; i++) {

Thread.sleep(1000);

dot+= ".";

System.out.println(dot);

}

}public static void main(String[] args) throwsIOException {new Thread(new AIOServer(8989, 19)).start();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值