BIO模型
同步并阻塞(传统阻塞型),服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销。
示意图:
在BIO模型中,每一个客户端与服务器的链接都会创建一个线程,无论客户端是否向服务器发送数据。
代码验证
public class BIOServer {
public static void main(String[] args) throws IOException {
//创建一个线程池
ExecutorService threadPool = Executors.newFixedThreadPool(10);
//创建一个server
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("服务器启动");
while (true) {
//等待客户端链接
final Socket socket = serverSocket.accept();
threadPool.execute(new Runnable() {
public void run() {
//可以和客户端通讯了
handler(socket);
}
});
}
}
public static void handler(Socket socket) {
try {
byte[] bytes = new byte[1024];
//获取socket的输入流
InputStream inputStream = socket.getInputStream();
while (true) {
//将输入流的内容读取到字节数组中,一次最多读取1024字节
int readCount = inputStream.read(bytes);
if (readCount != -1) {
String value = new String(bytes, 0, readCount);
System.out.println("客户端的数据:"+value);
System.out.println("当前客户端的线程ID: " + Thread.currentThread()
.getId() + " 当前线程Name: " + Thread.currentThread().getName());
}else break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我们使用telnet 127.0.0.1 6666
cmd命令来验证。
我们创建了两个客户端的链接并发送数据。
查看服务端的打印是每一个客户端都有一个线程的。如果客户端没有进行读写操作,则就造成了资源浪费。
NIO模型
同步非阻塞,服务器实现模式为一个线程处理多个请求(连接),即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求就进行处理。
示意图
在NIO模式下,服务端的一个线程对应一个选择器(Selector),而一个Selector会对应多个信道(Channel),每个信道都有自己的缓冲区(Buffer),在缓冲区中可以进行数据的读写。在NIO模式中,客户端是和Buffer交互的。如下图
测试案例写数据
将buffer中的数据读到channel,并写入到文件
public class NIOFileChannel {
public static void main(String[] args) throws IOException {
String str = "Hello,World";
//创建输出流
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.txt");
//获取输出流的Channel
FileChannel fileChannel = fileOutputStream.getChannel();
//创建一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//将数据放入到缓冲区
byteBuffer.put(str.getBytes());
//此时fileChannel要从byteBuffer中读取数据
//将byteBuffer置换为读模式
byteBuffer.flip();
//将byteBuffer写到fileChannel中去
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
}
测试读数据
public class NIOFileChannel02 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test.txt");
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//将通道中的数据放入到缓冲区
fileChannel.read(byteBuffer);
String s = new String(byteBuffer.array());
System.out.println(s);
fileInputStream.close();
}
}
总结如下:
Channel中的两个方法
-
write(ByteBuffer src)
这个方法通常用于channel的写操作,也就是将buffer中的数据写到channel中。注意,在调用此方法之前,应先调用buffer的flip()方法。 -
read(ByteBuffer dst)
那相对于write,这个方法自然就是将channel中的数据写入到buffer中。
Buffer的flip()和clear()方法。
flip方法
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
flip方法的主要作用就是控制position变量。当我们在往buffer中写数据时,每写入一个下标位置的数据,position就会+1,而读数据时同样由position控制。
用下面代码举例
public static void main(String[] args) {
//创建一个IntBuffer,size为7
IntBuffer intBuffer = IntBuffer.allocate(7);
for (int i = 0; i < 5; i++) {
intBuffer.put(i);
}
//将写切换为读模式
intBuffer.flip();
while (intBuffer.hasRemaining()){
System.out.println(intBuffer.get());
}
}
我们向buffer数组中放入了5个数,也就是此时的position应该指向数组下标为5的位置。debug看果然是
此时执行翻转操作,也就是想要读数据,我们看position的变化。
此时position变为了0,也就是又指向了数组的第一个位置,我们输出第一个元素后再查看其变化。
此时position就成了1。也就是在从buffer中读取数据之前一定要执行flip方法。
除此之外,flip还会执行 limit = position;
的操作,将position的值复给limit,这样就保证将来读取到的每一个下标都是有值的,如果使用capacity这个变量来判断是不行的,因为数组中的下标可能不会都有元素。
clear方法
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
clear方法的作用就是在将channel中的数据放入缓冲区时,如果需要循环多次放入,则下一次放入之前要调用clear方法,重置position,否则假设buffer的容量为10,第一次放满了10个容量的数据。如果不调用clear,那position就会大于limit。则会发生越界异常。