java bio与nio

bio的缺陷
Socket socket = server.accept(); //在等待客户端连接
InputStream fileIn = new FileInputStream(file);//等待客户端传入数据
在服务端等待客户端连接还有等待接收客户端信息时都会堵塞(一直等待无法进入下一步同时占用内存),因此在不采用多线程时bio无法处理并发 而多线程会有上下文切换的开销还会增加资源的消耗(cpu 内存等)

nio 实现了不堵塞的方法 先判断有没有客户端连接 有的话再判断有没有数据传入 同时客户连接存入list 方便每次读取数据时判断有没有之前的客户端传入数据

原理代码如下

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class MyServerNio {
	static byte[] bytes = new byte[1024];
	static List<SocketChannel> list = new ArrayList<SocketChannel>();
	static ByteBuffer byteBuffer = ByteBuffer.allocate(512);
	public static void main(String[] args) throws IOException, InterruptedException {
		ServerSocketChannel serverChannel = ServerSocketChannel.open();
		serverChannel.bind(new InetSocketAddress(8080));
		serverChannel.configureBlocking(false);//设置成非堵塞状态
		while(true) {
			SocketChannel socketChannel = serverChannel.accept();//接收请求
			if(socketChannel == null) {
				//未收到连接
				Thread.sleep(1500);//睡眠时间
				System.out.println("无人连接");
				for(SocketChannel client : list) {//实际开发中 是操作系统的函数 例如window的select() 但是还是会有资源浪费 只有linux中有epoll方法 会直接反馈有数据的连接
					int k = client.read(byteBuffer);
					if(k>0) {//判断有没有传入数据
					byteBuffer.flip();
					System.out.println(byteBuffer.toString());
					}
				}
			}else {
				//收到连接
				System.out.println("有连接");
				serverChannel.configureBlocking(false);//设置成非堵塞状态
				list.add(socketChannel);
				for(SocketChannel client : list) {
					int k = client.read(byteBuffer);
					if(k>0) {//判断有没有传入数据
					byteBuffer.flip();
					System.out.println(byteBuffer.toString());
					}
				}
			}	
		}
	}
}

发布了35 篇原创文章 · 获赞 0 · 访问量 650

猜你喜欢

转载自blog.csdn.net/Azadoo/article/details/105583907
今日推荐