快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

NIO的代码实现JAVA java nio代码

java nio 非阻塞读写具体应该怎么操作,能否给个例子程序

package com.java.xiong.Net17;

成都创新互联公司专注于涟水网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供涟水营销型网站建设,涟水网站制作、涟水网页设计、涟水网站官网定制、小程序开发服务,打造涟水网络公司原创品牌,更为您提供涟水网站排名全网营销落地服务。

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.Channel;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.nio.charset.Charset;

public class NServer {

// 用于检测所有的Channel状态的selector

private Selector selector = null;

static final int PORT = 30000;

// 定义实现编码、解码的字符串集对象

private Charset charse = Charset.forName("GBK");

public void init() throws IOException {

selector = Selector.open();

// 通过open方法来打开一个未绑定的ServerSocketChannel是咧

ServerSocketChannel server = ServerSocketChannel.open();

InetSocketAddress isa = new InetSocketAddress("127.0.0.1", PORT);

// 将该ServerSocketChannel绑定到指定的IP地址

server.bind(isa);

// 设置serverSocket已非阻塞方式工作

server.configureBlocking(false);

// 将server注册到指定的selector对象

server.register(selector, SelectionKey.OP_ACCEPT);

while (selector.select()  0) {

// 一次处理selector上的每个选择的SelectionKey

for (SelectionKey sk : selector.selectedKeys()) {

// 从selector上已选择的Kye集中删除正在处理的SelectionKey

selector.selectedKeys().remove(sk);

// 如果sk对应的Channel包含客户端的连接请求

if (sk.isAcceptable()) {

// 调用accept方法接收连接,产生服务器段的SocketChennal

SocketChannel sc = server.accept();

// 设置采用非阻塞模式

sc.configureBlocking(false);

// 将该SocketChannel注册到selector

sc.register(selector, SelectionKey.OP_READ);

}

// 如果sk对应的Channel有数据需要读取

if (sk.isReadable()) {

// 获取该SelectionKey对银行的Channel,该Channel中有刻度的数据

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

// 定义备注执行读取数据源的ByteBuffer

ByteBuffer buff = ByteBuffer.allocate(1024);

String content = "";

// 开始读取数据

try {

while (sc.read(buff)  0) {

buff.flip();

content += charse.decode(buff);

}

System.out.println("读取的数据:" + content);

// 将sk对应的Channel设置成准备下一次读取

sk.interestOps(SelectionKey.OP_READ);

}

// 如果捕获到该sk对银行的Channel出现了异常,表明

// Channel对应的Client出现了问题,所以从Selector中取消

catch (IOException io) {

// 从Selector中删除指定的SelectionKey

sk.cancel();

if (sk.channel() != null) {

sk.channel().close();

}

}

// 如果content的长度大于0,则连天信息不为空

if (content.length()  0) {

// 遍历selector里注册的所有SelectionKey

for (SelectionKey key : selector.keys()) {

// 获取该key对应的Channel

Channel targerChannel = key.channel();

// 如果该Channel是SocketChannel对象

if (targerChannel instanceof SocketChannel) {

// 将读取到的内容写入该Channel中

SocketChannel dest = (SocketChannel) targerChannel;

dest.write(charse.encode(content));

}

}

}

}

}

}

}

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

new NServer().init();

}

}

Java NIO怎么理解通道和非阻塞

nio引入了buffer、channel、selector等概念。

通道相当于之前的I/O流。

“通道”太抽象了。java解释不清的东西只能看它底层是怎么解释的——操作系统的I/O控制,通道控制方式?

I/O设备:CPU——通道——设备控制器——I/O设备

(通道和设备控制器的关系是多对多,设备控制器和I/O设备的关系也是多对多。)

I/O过程,参考;DMA.htm:

1.CPU在执行用户程序时遇到I/O请求,根据用户的I/O请求生成通道程序(也可以是事先编好的)。放到内存中,并把该通道程序首地址放入CAW中。

2.CPU执行“启动I/O”指令,启动通道工作。

3.通道接收“启动I/O”指令信号,从CAW(记录下一条通道指令存放的地址)中取出通道程序首地址,并根据此地址取出通道程序的第一条指令,放入CCW(记录正在执行的通道指令)中;同时向CPU发回答信号,通知“启动I/O”指令完成完毕,CPU可继续执行。

4.与此同时,通道开始执行通道程序,进行物理I/O操作。当执行完一条指令后,如果还有下一条指令则继续执行;否则表示传输完成,同时自行停止,通知CPU转去处理通道结束事件,并从CCW中得到有关通道状态。

如此一来,主处理器只要发出一个I/O操作命令,剩下的工作完全由通道负责。I/O操作结束后,I/O通道会发出一个中断请求,表示相应操作已完成。

通道控制方式是对数据块进行处理的,并非字节。

通道控制方式就是异步I/O,参考:

I/O分两段:1.数据从I/O设备到内核缓冲区。2.数据从内核缓冲区到应用缓冲区

I/O类型:

1.异步I/O不会产生阻塞,程序不会等待I/O完成,继续执行代码,等I/O完成了再执行一个什么回调函数,代码执行效率高。很容易联想到ajax。这个一般用于I/O操作不影响之后的代码执行。

2.阻塞I/O,程序发起I/O操作后,进程阻塞,CPU转而执行其他进程,I/O的两个步骤完成后,向CPU发送中断信号,进程就绪,等待执行。

3.非阻塞I/O并非都不阻塞,其实是第一步不阻塞,第二部阻塞。程序发起I/O操作后,进程一直检查第一步是否完成,CPU一直在循环询问,完成后,进程阻塞直到完成第二步。明白了!这个是“站着茅坑不拉屎”,CPU利用率最低的。逻辑和操作系统的程序直接控制方式一样。

阻塞不阻塞描述的是发生I/O时当前线程的状态。

以上是操作系统的I/O,那么java的nio又是怎样的呢?

个人觉得是模仿了通道控制方式。

先看看nio的示例代码:

服务端TestReadServer.java

import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; 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.util.Iterator; import java.util.Set; public class TestReadServer { /*标识数字*/ private int flag = 0; /*缓冲区大小*/ private int BLOCK = 1024*1024*10; /*接受数据缓冲区*/ private ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); /*发送数据缓冲区*/ private ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); private Selector selector; public TestReadServer(int port) throws IOException { // 打开服务器套接字通道 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 服务器配置为非阻塞 serverSocketChannel.configureBlocking(false); // 检索与此通道关联的服务器套接字 ServerSocket serverSocket = serverSocketChannel.socket(); // 进行服务的绑定 serverSocket.bind(new InetSocketAddress(port)); // 通过open()方法找到Selector selector = Selector.open(); // 注册到selector,等待连接 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Server Start----"+port+":"); } // 监听 private void listen() throws IOException { while (true) { // 选择一组键,并且相应的通道已经打开 selector.select(); // 返回此选择器的已选择键集。 SetSelectionKey selectionKeys = selector.selectedKeys(); IteratorSelectionKey iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); iterator.remove(); handleKey(selectionKey); } } } // 处理请求 private void handleKey(SelectionKey selectionKey) throws IOException { // 接受请求 ServerSocketChannel server = null; SocketChannel client = null; String receiveText; String sendText; int count=0; // 测试此键的通道是否已准备好接受新的套接字连接。 if (selectionKey.isAcceptable()) { // 返回为之创建此键的通道。 server = (ServerSocketChannel) selectionKey.channel(); // 接受到此通道套接字的连接。 // 此方法返回的套接字通道(如果有)将处于阻塞模式。 client = server.accept(); // 配置为非阻塞 client.configureBlocking(false); // 注册到selector,等待连接 client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { // 返回为之创建此键的通道。 client = (SocketChannel) selectionKey.channel(); //将缓冲区清空以备下次读取 receivebuffer.clear(); //读取服务器发送来的数据到缓冲区中 System.out.println(System.currentTimeMillis()); count = client.read(receivebuffer); System.out.println(System.currentTimeMillis() + "~"+count); } } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int port = 1234; TestReadServer server = new TestReadServer(port); server.listen(); } }客户端TestReadClient.javaimport java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; public class TestReadClient { /*标识数字*/ private static int flag = 0; /*缓冲区大小*/ private static int BLOCK = 1024*1024*10; /*接受数据缓冲区*/ private static ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); /*发送数据缓冲区*/ private static ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); /*服务器端地址*/ private final static InetSocketAddress SERVER_ADDRESS = new InetSocketAddress( "localhost", 1234); public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // 打开socket通道 SocketChannel socketChannel = SocketChannel.open(); // 设置为非阻塞方式 socketChannel.configureBlocking(false); // 打开选择器 Selector selector = Selector.open(); // 注册连接服务端socket动作 socketChannel.register(selector, SelectionKey.OP_CONNECT); // 连接 socketChannel.connect(SERVER_ADDRESS); // 分配缓冲区大小内存 SetSelectionKey selectionKeys; IteratorSelectionKey iterator; SelectionKey selectionKey; SocketChannel client; String receiveText; String sendText; int count=0; while (true) { //选择一组键,其相应的通道已为 I/O 操作准备就绪。 //此方法执行处于阻塞模式的选择操作。 selector.select(); //返回此选择器的已选择键集。 selectionKeys = selector.selectedKeys(); //System.out.println(selectionKeys.size()); iterator = selectionKeys.iterator(); while (iterator.hasNext()) { selectionKey = iterator.next(); if (selectionKey.isConnectable()) { System.out.println("client connect"); client = (SocketChannel) selectionKey.channel(); // 判断此通道上是否正在进行连接操作。 // 完成套接字通道的连接过程。 if (client.isConnectionPending()) { client.finishConnect(); System.out.println("完成连接!"); sendbuffer.clear(); BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File("D:\BigData.zip"))); byte[] b = new byte[BLOCK]; br.read(b); sendbuffer.put(b); sendbuffer.flip(); System.out.println(System.currentTimeMillis()); client.write(sendbuffer); System.out.println(System.currentTimeMillis()); } client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { client = (SocketChannel) selectionKey.channel(); //将缓冲区清空以备下次读取 receivebuffer.clear(); //读取服务器发送来的数据到缓冲区中 count=client.read(receivebuffer); if(count0){ receiveText = new String( receivebuffer.array(),0,count); System.out.println("客户端接受服务器端数据--:"+receiveText); client.register(selector, SelectionKey.OP_WRITE); } } } selectionKeys.clear(); } } }例子是TestReadClient向TestReadServer发送一个本地文件。TestReadServer收到后每次打印读取到的字节数。

如何体现异步I/O?

看看TestReadClient中的:

if (selectionKey.isConnectable()) { System.out.println("client connect"); client = (SocketChannel) selectionKey.channel(); // 判断此通道上是否正在进行连接操作。 // 完成套接字通道的连接过程。 if (client.isConnectionPending()) { client.finishConnect();如果没有client.finishConnect();这句等待完成socket连接,可能会报异常:java.nio.channels.NotYetConnectedException

异步的才不会管你有没有连接成功,都会执行下面的代码。这里需要人为的干预。

如果要证明是java的nio单独使用非阻塞I/O,真没办法!!!阻塞非阻塞要查看进程。。。

不过还有种说法,叫异步非阻塞。上面那段,是用异步方式创建连接,进程当然没有被阻塞。使用了finishConnect()这是人为将程序中止,等待连接创建完成(是模仿阻塞将当前进程阻塞掉,还是模仿非阻塞不断轮询访问,不重要了反正是程序卡住没往下执行)。

所以,创建连接的过程用异步非阻塞I/O可以解释的通。那read/write的过程呢?

根据上面例子的打印结果,可以知道这个过程是同步的,没执行完是不会执行下面的代码的。至于底下是使用阻塞I/O还是非阻塞I/O,对于应用级程序来说不重要了。

阻塞还是非阻塞,对于正常的开发(创立连接,从连接中读写数据)并没有多少的提升,操作过程都类似。

那NIO凭什么成为高性能架构的基础,比起IO,性能优越在哪里,接着猜。。。

java nio有意模仿操作系统的通道控制方式,那他的底层是不是就是直接使用操作系统的通道?

通道中的数据是以块为单位的,之前的流是以字节为单位的,同样的数据流操作外设的次数较多。代码中channel都是针对ByteBuffer对象进行read/write的,而ByteBuffer又是ByteBuffer.allocate(BLOCK);这样创建的,是一个连续的块空间。

那ByteBuffer是不是也是模拟操作系统的缓存?

缓存在io也有,如BufferedInputStream。CPU和外设的速度差很多,缓存为了提高CPU使用率,等外设将数据读入缓存后,CPU再统一操作,不用外设读一次,CPU操作一次,CPU的效率会被拉下来。。。

利用java.nio的FileChannel能够实现按行读取文件吗?(解决了)

利用java.nio的FileChannel能够实现按行读取文件:

具体思路是:设置两个缓冲区,一大一小,大的缓冲区为每次读取的量,小的缓冲区存放每行的数据(确保大小可存放文本中最长的那行)。读取的时候判断是不是换行符13,是的话则返回一行数据,不是的话继续读取,直到读完文件。

实现方法:

FileChannel fc=raf.getChannel();

//一次读取文件,读取的字节缓存数

ByteBuffer fbb=ByteBuffer.allocate(1024*5);

fc.read(fbb);

fbb.flip();

//每行缓存的字节   根据你的实际需求     

ByteBuffer bb=ByteBuffer.allocate(500);

//判断是否读完文件

public boolean hasNext() throws IOException {

if(EOF)return false;

if(fbb.position()==fbb.limit()){//判断当前位置是否到了缓冲区的限制

if(readByte()==0)  return false;

}

while(true){

if(fbb.position()==fbb.limit()){

if(readByte()==0)  break;

}

byte a=fbb.get();

if(a==13){

if(fbb.position()==fbb.limit()){

if(readByte()==0)  break;

}

return true;

}else{

if (bb.position()  bb.limit()) {

bb.put(a);

}else {

if(readByte()==0)  break;

}

}

}

return true;

}


新闻标题:NIO的代码实现JAVA java nio代码
本文路径:http://6mz.cn/article/dogdchd.html

其他资讯