linux的IO模型:
- 阻塞IO
- 非阻塞IO
- IO复用
- 信号驱动IO
- 异步IO
IO和NIO的区别:
- IO面向流,NIO面向缓冲区
- IO阻塞,NIO非阻塞
nio中channel和buffer
channel可以理解为对socket的一种抽象, channel类似于流,可以读写到buffer中。
在nio中,使用buffer读写数据一般遵循以下四个步骤:
1、写入数据到Buffer
2.调用flip方法,将buffer从写模式切换到读模式
3.从Buffer中读取数据
4.调用clear方法或者compact方法
为了理解buffer的工作原理,需要弄明白三个概念
capacity
position
limit
这几个概念结合读模式和写模式就很好去理解了。
capacity
作为一个内存块,Buffer有一个固定的大小值, 也叫“capacity”.你只能往里写capacity个byte、long,char等类型。 一旦Buffer满了,需要将其清空(通过读数据或者清除数据)才能继续写数据往里写数据。
position
当你写数据到Buffer中时,position表示当前的位置。 初始的position值为0.当一个byte、long等数据写到Buffer后, position会向前移动到下一个可插入数据的Buffer单元。 position最大可为capacity – 1. 当读取数据时,也是从某个特定位置读。 当将Buffer从写模式切换到读模式,position会被重置为0. 当从Buffer的position处读取数据时,position向前移动到下一个可读的位置。
limit
在写模式下,Buffer的limit表示你最多能往Buffer里写多少数据。 写模式下,limit等于Buffer的capacity。 当切换Buffer到读模式时, limit表示你最多能读到多少数据。 因此,当切换Buffer到读模式时,limit会被设置成写模式下的position值。 换句话说,你能读到之前写入的所有数据(limit被设置成已写数据的数量,这个值在写模式下就是position)
clear()与compact()方法
一旦读完Buffer中的数据,需要让Buffer准备好再次被写入。 可以通过clear()或compact()方法来完成。
如果调用的是clear()方法,position将被设回0,limit被设置成 capacity的值。 换句话说,Buffer 被清空了。 Buffer中的数据并未清除,只是这些标记告诉我们可以从哪里开始往Buffer里写数据。 如果Buffer中有一些未读的数据,调用clear()方法,数据将“被遗忘”, 意味着不再有任何标记会告诉你哪些数据被读过,哪些还没有。
如果Buffer中仍有未读的数据,且后续还需要这些数据,但是此时想要先先写些数据,那么使用compact()方法。 compact()方法将所有未读的数据拷贝到Buffer起始处。 然后将position设到最后一个未读元素正后面。limit属性依然像clear()方法一样, 设置成capacity。现在Buffer准备好写数据了,但是不会覆盖未读的数据。
Netty中的ByteBuf
读写netty中的ByteBuf就不需要flip操作,是因为它自身有两个指针,一个对应读操作,一个对应写操作。 当你向ByteBuf里写入数据的时候写指针的索引就会增加,同时读指针的索引没有变化。
####
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.clear.temp;
import org.apache.ibatis.annotations.SelectKey;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* User: xuguangwu
* Date: 2018/11/20
* Time: 9:15 PM
*/
public class NioServer {
//多路复用选择器
private static Selector selector;
private static ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10));
static ServerSocketChannel serverSocketChannel;
public static void main(String[] args) throws Exception {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(new InetSocketAddress(8080));
selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterable = selectionKeys.iterator();
while (iterable.hasNext()) {
SelectionKey key = iterable.next();
iterable.remove();
handle(key);
}
}
}
private static void handle(SelectionKey key) throws Exception {
if (key.isAcceptable()) {
doAccept(key);
}
if (key.isConnectable()) {
doConnect(key);
}
}
private static void doConnect(SelectionKey key) {
}
private static void doAccept(SelectionKey key) throws Exception {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("监听accept");
serverSocketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}