java AIO之学习笔记

同步和异步都是基于应用程序和操作系统处理的IO时间锁采用的方式,比如同步应用程序要直接参与IO读写的操作,异步:所有的IO读写交给操作系统去处理。同步的方式在处理IO事件的时候必须阻塞在摸个方法上面等待我们的IO时间完成(阻塞IO事件或者通过轮询IO事件的方式),对于异步所有IO读写都交给了操作系统,此时我们可以去做其他的事情,并不需要去完成真正的IO操作,等操作系统完成IO操作,给我们应用程序一个通知就可以了。

同步:1)阻塞到IO事件,阻塞到read或者write方法上面,此时我们不能做自己的事情。补救方法:让这些读写方法加入到线程里面,然后阻塞线程来实现,对线程性能开销比较大。

  2IO事件的轮询  多路复用技术(select模式)

  读写事件交给一个单独的线程来处理,这个线程完成IO事件的注册功能,还有就是不断的轮询我们的读写缓冲区,看是否有数据准备好,通知相应的读写事件,这样的话 以前的读写线程就可以做其他的事情。


NIO:通过selector(选择器)就相当于管家,管理所有的IO事件 connection accept

客户端和服务端的读写。

Selector(选择器)如何进行管理IO事件

IO事件注册给我们 的选择器的时候,选择器会给我们分配一个key(简单理解成一个事件的标签)当IO事件完成,通过key值来找相应的管道,然后通过管道来发送和接受数据等操作。

 

数据缓冲区:

通过bytebuffer,提供很多读写的方法 put() get()

服务端:serverSocketChannel

客户端:socketChannel

选择器:Selector select=Selector.Open()  打开我们的选择器。

Selectionkey:判断IO事件是否已经就绪。

 

AIO

服务端:AsynchronousServerSocketChannel

客户端:ASynchronousSocketChannel

用户处理器:completionHandler接口,这个接口实现应用程序向操作系统发起IO请求,当完成后处理具体逻辑,否则做自己该做的事情。


案例分析:

服务端

package it.com.Jerome;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousServerSocketChannel;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;

import java.util.concurrent.ExecutionException;

public class AIOServer {

public AIOServer(int port) throws IOException{

final AsynchronousServerSocketChannel listener=AsynchronousServerSocketChannel.open()

.bind(new InetSocketAddress(port));

listener.accept(null,new CompletionHandler<AsynchronousSocketChannel,Void>(){

 

@Override

public void completed(AsynchronousSocketChannel ch, Void attachment) {

// TODO Auto-generated method stub

listener.accept(null, this);//接受下一个链接

try {

handler(ch);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

@Override

public void failed(Throwable exc, Void attachment) {

// TODO Auto-generated method stub

System.out.println("AIo失败!");

}

 

});

}

//真正逻辑处理

public void handler(AsynchronousSocketChannel ch) throws Exception, ExecutionException{

ByteBuffer bytebuffer=ByteBuffer.allocate(32);

ch.read(bytebuffer).get();

bytebuffer.flip();

System.out.println("服务端接受数据"+bytebuffer.get());

}

public static void main(String[] args) throws Exception {

AIOServer server =new AIOServer(7080);

System.out.println("监听端口7080");

Thread.sleep(10000);

}

}

客户端:

package it.com.Jerome;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.util.concurrent.Future;

public class AIOClient {

private AsynchronousSocketChannel client=null;

public AIOClient(String host,int port) throws Exception{

client =AsynchronousSocketChannel.open();

Future<?> future=client.connect(new InetSocketAddress(host,port));

System.out.println(future.get());

}

public void write(byte b){

ByteBuffer bytebuffer=ByteBuffer.allocate(32);

bytebuffer.put(b);

bytebuffer.flip();

client.write(bytebuffer);

}

public static void main(String[] args) throws Exception {

AIOClient client=new AIOClient("localhost",7080);

client.write((byte)29);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值