Channel有一点类似于stream,它就是读写数据的双向通道,可以从channel将数据读入buffer,也可以将buffer的数据写入channel,而之前的stream要么是输入,要么是输出,channel比stream更为底层。
常见的Channel有
buffer则用于缓冲读写数据,常见的buffer有
selector单从字面意思不好理解,需要结合服务器的设计演化来理解它的用途
selector的作用就是配合一个线程来管理多个channel,获取这些channel上发生的事件,这些channel工作在非阻塞模式下,不会让线程吊死在一个channel上,适合连接数特别多,但流量低的场景
调用selector的select()会阻塞直到channel发生了读写就绪事件,这些事件发生,select方法就会返回这些事件交给thread来处理
1.向buffer写入数据,例如调用channel.read(buffer)
2.调用flip()切换至读模式
3.从buffer读取数据,例如调用buffer.get()
4.调用clear()或compact()切换至写模式
5.重复1~4步骤
代码:
@Slf4j
public class TestByteBuffer {
public static void main(String[] args) {
try {
//FileChannel
FileChannel channel = new FileInputStream("data.txt").getChannel();
//准备缓冲区
ByteBuffer buffer = ByteBuffer.allocate(10);
//从FileChannel读取数据,向buffer写入
while(true){
int len = channel.read(buffer);
log.debug("读取到的字节数 {}",len);
if(len == -1){ //没有内容
break;
}
//打印buffer的内容
buffer.flip(); //切换至读模式
while (buffer.hasRemaining()) { //是否还有剩余未读数据
byte b = buffer.get();
log.debug("实际字节 {}",(char) b);
}
buffer.clear(); //切换为写模式
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ByteBuffer有以下重要属性
一开始
写模式下,position是写入位置,limit等于容量,下图表示写入了4个字节后的状态
flip(读)动作发生后,position切换为读取位置,limit切换为读取限制
读取4个字节后,状态
clear(写)动作发生后,状态
compact方法,是把未读完的部分向前压缩,然后切换至写模式
可以使用allocate或者allocateDirect方法为ByteBuffer分配空间,其他buffer类也有该方法
Bytebuffer buf = ByteBuffer.allocate(16);
Bytebuffer buf = ByteBuffer.allocateDirect(16)
区别:
class java.nio.HeapByteBuffer:java堆内存,读写效率较低,受到GC的影响。
class java.nio.DirectByteBuffer:直接内存,读写效率高(少一次拷贝),不会受到GC影响,分配的效率低。
有两种方法
int readBytes = channel.read(buf);
和
buf.put((byte)127);
同样有两种方法
int writeBytes = channel.write(buf);
和
byte b = buf.get();
get方法会让position读指针向后走,如果想重复读取数据
rewind:
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[]{'a','b','c','d'});
buffer.flip();
//rewind从头开始读
buffer.get(new byte[4]);
debugAll(buffer);
buffer.rewind();
debugAll(buffer);
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.mark(); //加标记,索引2的位置
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.reset(); //是将position重置到索引2
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
System.out.println((char) buffer.get(3));
debugAll(buffer);
代码:
//1.字符串转为ByteBuffer
ByteBuffer buffer1 = ByteBuffer.allocate(16);
buffer1.put("hello".getBytes());
debugAll(buffer1);
//2.Charset
ByteBuffer buffer2 = Charset.forName("UTF-8").encode("hello");
debugAll(buffer2);
//3.wrap
ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
debugAll(buffer3);
//4.转为字符串
String str1 = Charset.forName("UTF-8").decode(buffer2).toString();
System.out.println(str1);
buffer1.flip();
String str2 = Charset.forName("UTF-8").decode(buffer1).toString();
System.out.println(str2);
分散读取,有一个文本文件words.txt
onetwothree
使用如下方式读取,可以将数据填充至多个buffer
try {
FileChannel channel = new RandomAccessFile("words.txt", "r").getChannel();
ByteBuffer b1 = ByteBuffer.allocate(3);
ByteBuffer b2 = ByteBuffer.allocate(3);
ByteBuffer b3 = ByteBuffer.allocate(5);
channel.read(new ByteBuffer[]{b1,b2,b3});
b1.flip();
b2.flip();
b3.flip();
debugAll(b1);
debugAll(b2);
debugAll(b3);
} catch (IOException e) {
e.printStackTrace();
}
使用如下方式写入,可以将多个buffer中的数据写入到一个文件
ByteBuffer b1 = Charset.forName("UTF-8").encode("hello");
ByteBuffer b2 = Charset.forName("UTF-8").encode("world");
ByteBuffer b3 = Charset.forName("UTF-8").encode("您好");
try {
FileChannel channel = new RandomAccessFile("words2.txt", "rw").getChannel();
channel.write(new ByteBuffer[]{b1,b2,b3});
} catch (IOException e) {
e.printStackTrace();
}
题目:网络上有多条数据发送给服务端,数据之间使用\n进行分割,但由于某种原因这些数据在接收时,被进行了重新组合,例如原始数据有3条为
Hello,world\n
I’m zhangsan\n
How are you?\n
变成了下面的两个byteBuffer(黏包,半包)
Hello,world\nI’m zhangsan\nHo
w are you?\n
现在要求你编写程序,将错乱的数据恢复成原始的按\n分割的数据
public static void main(String[] args) {
ByteBuffer source = ByteBuffer.allocate(32);
source.put("Hello,world\nI'm zhangsan\nHo".getBytes());
split(source);
source.put("w are you?\n".getBytes());
split(source);
}
private static void split(ByteBuffer source){
source.flip();
for(int i=0;i<source.limit();i++){
//找到一条完整消息
if(source.get(i)=='\n'){
int len = i + 1 - source.position();
//把这条完整信息存入新的ByteBuffer
ByteBuffer target = ByteBuffer.allocate(len);
for(int j = 0; j < len; j++){
target.put(source.get());
}
debugAll(target);
}
}
source.compact();
}
FileChannel只能工作在阻塞模式下
不能直接打开FileChannel,必须通过FileInputStream、FileOutputStream或者RandomAccessFile来获取FileChannel,它们都有getChannel方法
会从channel读取数据填充ByteBuffer,返回值表示读到了多少字节,-1表示到达了文件的末尾
int readBytes = channel.read(buffer);
写入的正确姿势如下,
ByteBuffer buffer = ...;
buffer.put(...);
buffer.flip(); //切换读模式
while(buffer.hasRemaining()){
channel.write(buffer);
}
在while中调用channel.write是因为write方法并不能保证一次将buffer中的内容全部写入channel
channel必须关闭,不过调用了FileInputStream、FileOuputStream或者RandomAccessFile的close方法会间接地调用channel的close方法
获取当前位置
long pos = channel.position();
设置当前位置
long newPos = ...;
channel.position(newPos);
设置当前位置时,如果设置为文件的末尾
使用size方法获取文件的大小
操作系统出于性能的考虑,会将数据缓存,不是立刻写入磁盘。可以调用force(true)方法将文件内容和元数据(文件的权限等信息)立刻写入磁盘。
try {
FileChannel from = new FileInputStream("data.txt").getChannel();
FileChannel to = new FileOutputStream("to.txt").getChannel();
//效率高,底层会利用操作系统的零拷贝优化,2g数据
long size = from.size();
for (long left = size;left >0;) {
left -= from.transferTo(size-left,left,to);
}
from.close();
to.close();
} catch (IOException e) {
e.printStackTrace();
}
jdk7引入了Path和Paths类
Path source = Paths.get("1.txt"); //相对路径,使用user.dir环境变量来定位1.txt
Path source = Paths.get("d://1.txt"); //绝对路径,代表了d:\1.txt
Path source = Paths.get("d:/1.txt"); //绝对路径,代表了d:\1.txt
Path source = Paths.get("d://data","projects"); //代表了d:\data\projects
代码
Path path = Paths.get("d:\\data\\projects\\a\\..\\b");
System.out.println(path);
System.out.println(path.normalize); //正常化路径
会输出
d:\data\projects\a\..\b
d:\data\projects\b
Path path = Paths.get("to.txt");
System.out.println(Files.exists(path));
Path path = Paths.get("d1");
Files.createDirectory(path);
Path path = Paths.get("d1/d2");
Files.createDirectories(path);
Path source = Paths.get("helloword/data.txt");
Path target= Paths.get("helloword/target.txt");
Files.copy(source,target);
如果希望用source覆盖掉target,需要用StandardCopyOption来控制
Files.copy(source,target,StandardCopyOption.REPLACE_EXISTING);
Path source = Paths.get("helloword/data.txt");
Path target= Paths.get("helloword/target.txt");
Files.move(source,target,StandardCopyOption.ATOMIC_MOVE);
Path target= Paths.get("helloword/target.txt");
Files.delete(target);
Path target= Paths.get("helloword/d1");
Files.delete(target);
代码:
AtomicInteger dirCount = new AtomicInteger(); //不能使用int变量,匿名内部类必须使用final修饰的变量,否则会产生二义性
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(Paths.get("C:\\Program Files\\Java\\jdk1.8.0_201"),new SimpleFileVisitor<Path>(){
//访问文件夹前
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("===>"+dir);
dirCount.getAndIncrement();
return super.preVisitDirectory(dir, attrs);
}
//访问文件夹后
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return super.postVisitDirectory(dir, exc);
}
//访问文件
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
fileCount.getAndIncrement();
return super.visitFile(file, attrs);
}
//访问文件失败
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return super.visitFileFailed(file, exc);
}
});
System.out.println("文件夹的个数:"+dirCount);
System.out.println("文件的个数:"+fileCount);
代码:
Files.walkFileTree(Paths.get("C:\\Program Files\\Java\\jdk1.8.0_201"),new SimpleFileVisitor<Path>(){
//访问文件夹前
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return super.preVisitDirectory(dir, attrs);
}
//访问文件夹后
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
//访问文件
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
//访问文件失败
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return super.visitFileFailed(file, exc);
}
});
代码:
String source = "C:\\Users\\19088\\Desktop\\spring-security-master";
String target = "C:\\Users\\19088\\Desktop\\spring-security-master11";
Files.walk(Paths.get(source)).forEach(path -> {
try {
String targetName = path.toString().replace(source,target);
//是目录
if(Files.isDirectory(path)){
Files.createDirectory(Paths.get(targetName));
}
//是文件
if(Files.isRegularFile(path)){
Files.copy(path,Paths.get(targetName));
}
} catch (IOException e) {
e.printStackTrace();
}
});
服务器端:
//使用nio来理解阻塞模式,单线程
//0.ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(16);
//1.创建了服务器
ServerSocketChannel ssc = ServerSocketChannel.open();
//2.绑定监听端口
ssc.bind(new InetSocketAddress(8080));
//3.连接集合
List<SocketChannel> channels = new ArrayList<>();
while(true){
//4.accpet建立与客户端连接,SocketChannel用来与客户端通信
log.debug("connecting...");
SocketChannel sc = ssc.accept(); //阻塞方法,线程停止运行
log.debug("connected... {}",sc);
channels.add(sc);
for (SocketChannel channel: channels) {
//5.接收客户端发送的数据
log.debug("before read... {}",channel);
channel.read(buffer); //阻塞方法,线程停止运行
buffer.flip();
debugRead(buffer);
buffer.clear();
log.debug("after read... {}",channel);
}
}
客户端:
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
System.out.println("waiting...");
服务器端,客户端代码不变
//使用nio来理解非阻塞模式,单线程
//0.ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(16);
//1.创建了服务器
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false); //非阻塞模式
//2.绑定监听端口
ssc.bind(new InetSocketAddress(8080));
//3.连接集合
List<SocketChannel> channels = new ArrayList<>();
while(true){
//4.accpet建立与客户端连接,SocketChannel用来与客户端通信
SocketChannel sc = ssc.accept(); //非阻塞,线程还会继续运行,如果没有连接建立,但sc是null
if(sc != null) {
log.debug("connected... {}", sc);
sc.configureBlocking(false); //非阻塞方法
channels.add(sc);
}
for (SocketChannel channel: channels) {
//5.接收客户端发送的数据
int read = channel.read(buffer); //非阻塞,线程仍然会继续运行,如果没有读到数据,read返回0
if(read > 0) {
buffer.flip();
debugRead(buffer);
buffer.clear();
log.debug("after read... {}", channel);
}
}
}
单线程可以配合Selector完成对多个Channel可读写事件的监控,这称之为多路复用
Selector selector = Selector.open();
也称之为注册事件,绑定的事件selector才会关心
channel.configureBlocking(false);
SelectionKey key = channel.register(selector,绑定事件);
可以通过下面三种方法来监听是否有事件发生,方法的返回值代表有多少channel发生了事件
方法1,阻塞直到绑定事件发生
int count = selector.select();
方法2,阻塞直到绑定事件发生,或是超时(时间单位为ms)
int count = selector.select(long timeout);
方法3,不会阻塞,也就是不管有没有事件,立刻返回,自己根据返回值检查是否有事件
int count = selector.selectNow();
客户端代码为
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
System.out.println("waiting...");
在dubug模式开启即可
服务器端代码为
@Slf4j
public class ChannelDemo6 {
public static void main(String[] args) {
try (ServerSocketChannel channel = ServerSocketChannel.open()) {
channel.bind(new InetSocketAddress(8080));
System.out.println(channel);
Selector selector = Selector.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int count = selector.select();
log.debug("select count: {}", count);
// 获取所有事件
Set<SelectionKey> keys = selector.selectedKeys();
// 遍历所有事件,逐一处理
Iterator<SelectionKey> iter = keys.iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
// 判断事件类型
if (key.isAcceptable()) {
ServerSocketChannel c = (ServerSocketChannel) key.channel();
// 必须处理
SocketChannel sc = c.accept();
log.debug("{}", sc);
}
// 处理完毕,必须将事件移除
iter.remove();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
事件发生后,要么处理,要么取消(cancel),不能什么都不做,否则下次该事件仍会触发,这是因为nio底层使用的是水平触发
@Slf4j
public class Server {
public static void main(String[] args) throws IOException {
//1.创建selector,管理多个channel
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
//2.建立selector和channel的联系(注册)
//SelectionKey就是将来事件发生后,通过它可以知道事件和哪个channel的事件
SelectionKey sscKey = ssc.register(selector, 0, null);
//key只关注accept事件
sscKey.interestOps(SelectionKey.OP_ACCEPT);
log.debug("register key:{}",sscKey);
ssc.bind(new InetSocketAddress(8080));
while(true){
//3.select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,事件发生后要么处理,要么取消(key.cancel),不能置之不理
selector.select();
//4.处理事件,selectedKeys内部包含了所有发生的事件
Iterator<SelectionKey> iter = selector.selectedKeys().iterator(); //accept,read
while(iter.hasNext()){
SelectionKey key = iter.next();
//处理key的时候,要从selectedKeys集合中删除,否则下次处理就会有问题
iter.remove();
log.debug("key:{}",key);
//5.区分事件类型
if (key.isAcceptable()) { //如果是accept
ServerSocketChannel channel = (ServerSocketChannel)key.channel();
SocketChannel sc = channel.accept();
sc.configureBlocking(false);
SelectionKey sckey = sc.register(selector, 0, null);
sckey.interestOps(SelectionKey.OP_READ);
log.debug("{}",sc);
}else if(key.isReadable()){ //如果是read
try {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(4);
int read = channel.read(buffer); //如果是正常断开,read的方法的返回值是-1
if(read == -1){
key.cancel();
}else{
buffer.flip();
//debugRead(buffer);
System.out.println(Charset.forName("UTF-8").decode(buffer));
}
} catch (IOException e) {
e.printStackTrace();
key.cancel(); //因为客户端断开了,因此需要将key取消(从selector的keys集合中真正删除key)
}
}
}
}
}
}
因为select在事件发生后,就会将相关的key放入selectedKeys集合,但不会在处理完后从selectedKeys集合中移除,需要我们自己编码删除,例如
cancel会取消注册在selector上的channel,并从iter集合中删除key后续不会在监听事件
服务器端代码
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(9000);
while (true) {
Socket s = ss.accept();
InputStream in = s.getInputStream();
// 这里这么写,有没有问题
byte[] arr = new byte[4];
while(true) {
int read = in.read(arr);
// 这里这么写,有没有问题
if(read == -1) {
break;
}
System.out.println(new String(arr, 0, read));
}
}
}
}
客户端代码
public class Client {
public static void main(String[] args) throws IOException {
Socket max = new Socket("localhost", 9000);
OutputStream out = max.getOutputStream();
out.write("hello".getBytes());
out.write("world".getBytes());
out.write("你好".getBytes());
max.close();
}
}
输出
hell
owor
ld�
�好
扩容(以分隔符拆分为例):
服务器端代码
@Slf4j
public class Server {
private static void split(ByteBuffer source){
source.flip();
for(int i=0;i<source.limit();i++){
//找到一条完整消息
if(source.get(i)=='\n'){
int len = i + 1 - source.position();
//把这条完整信息存入新的ByteBuffer
ByteBuffer target = ByteBuffer.allocate(len);
for(int j = 0; j < len; j++){
target.put(source.get());
}
debugAll(target);
}
}
source.compact();
}
public static void main(String[] args) throws IOException {
//1.创建selector,管理多个channel
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
//2.建立selector和channel的联系(注册)
//SelectionKey就是将来事件发生后,通过它可以知道事件和哪个channel的事件
SelectionKey sscKey = ssc.register(selector, 0, null);
//key只关注accept事件
sscKey.interestOps(SelectionKey.OP_ACCEPT);
log.debug("register key:{}",sscKey);
ssc.bind(new InetSocketAddress(8080));
while(true){
//3.select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,事件发生后要么处理,要么取消(key.cancel),不能置之不理
selector.select();
//4.处理事件,selectedKeys内部包含了所有发生的事件
Iterator<SelectionKey> iter = selector.selectedKeys().iterator(); //accept,read
while(iter.hasNext()){
SelectionKey key = iter.next();
//处理key的时候,要从selectedKeys集合中删除,否则下次处理就会有问题
iter.remove();
log.debug("key:{}",key);
//5.区分事件类型
if (key.isAcceptable()) { //如果是accept
ServerSocketChannel channel = (ServerSocketChannel)key.channel();
SocketChannel sc = channel.accept();
sc.configureBlocking(false);
//将一个bytebuffer作为附件关联到selectorKey上
ByteBuffer buffer = ByteBuffer.allocate(16);
SelectionKey sckey = sc.register(selector, 0, buffer);
sckey.interestOps(SelectionKey.OP_READ);
log.debug("{}",sc);
}else if(key.isReadable()){ //如果是read
try {
SocketChannel channel = (SocketChannel) key.channel();
//获取selectionKey上关联的附件
ByteBuffer buffer = (ByteBuffer) key.attachment();
int read = channel.read(buffer); //如果是正常断开,read的方法的返回值是-1
if(read == -1){
key.cancel();
}else{
split(buffer);
if(buffer.position() == buffer.limit()){
ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2);
buffer.flip();
newBuffer.put(buffer);
key.attach(newBuffer);
}
}
} catch (IOException e) {
e.printStackTrace();
key.cancel(); //因为客户端断开了,因此需要将key取消(从selector的keys集合中真正删除key)
}
}
}
}
}
}
客户端代码
public class Client {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
SocketAddress address = sc.getLocalAddress();
sc.write(Charset.defaultCharset().encode("0123456789abcdef3333\n"));
System.in.read();
}
}
服务器端代码
public class WriteServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
ssc.bind(new InetSocketAddress(8080));
while(true){
selector.select();
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
if (key.isAcceptable()) {
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
SelectionKey sckey = sc.register(selector,0,null);
sckey.interestOps(SelectionKey.OP_READ);
//1.向客户端发送大量数据
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5000000; i++) {
sb.append("a");
}
ByteBuffer buffer = Charset.defaultCharset().encode(sb.toString());
//2.返回值代表实际写入的字节数
int write = sc.write(buffer);
System.out.println(write);
//3.判断是否还有剩余内容
if(buffer.hasRemaining()){
//4.关注可写事件
sckey.interestOps(sckey.interestOps() + SelectionKey.OP_WRITE);
//5.把未写完的数据挂到sckey上
sckey.attach(buffer);
}
}else if(key.isWritable()){
ByteBuffer buffer = (ByteBuffer) key.attachment();
SocketChannel sc = (SocketChannel) key.channel();
int write = sc.write(buffer);
System.out.println(write);
//6.清理操作
if(!buffer.hasRemaining()){
key.attach(null); //需要清除buffer
key.interestOps(key.interestOps() - SelectionKey.OP_WRITE); //不需关注可写事件
}
}
}
}
}
}
客户机端代码
public class WriteClient {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
//3.接收数据
int count = 0;
while(true){
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
count += sc.read(buffer);
System.out.println(count);
buffer.clear();
}
}
}
只要向 channel 发送数据时,socket 缓冲可写,这个事件会频繁触发,因此应当只在 socket 缓冲区写下时再关注可写事件,数据写完之后再取消关注
现在都是多核cpu,设计时要充分考虑别让cpu的力量被白白浪费
前面的代码只有一个选择器,没有充分利用多核cpu,如何改进呢?
分两组选择器
服务器端代码
@Slf4j
public class MultiThreadServer {
public static void main(String[] args) throws IOException {
Thread.currentThread().setName("boss");
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
Selector boss = Selector.open();
SelectionKey bossKey = ssc.register(boss, 0, null);
bossKey.interestOps(SelectionKey.OP_ACCEPT);
ssc.bind(new InetSocketAddress(8080));
//1.创建固定数量的worker,并初始化
Worker[] workers = new Worker[Runtime.getRuntime().availableProcessors()];
for (int i = 0; i < workers.length; i++) {
workers[i] = new Worker("worker-" + i);
}
AtomicInteger index = new AtomicInteger();
while (true) {
boss.select();
Iterator<SelectionKey> iter = boss.selectedKeys().iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
if (key.isAcceptable()) {
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
log.debug("connected...{}", sc.getRemoteAddress());
//2.关联selector
log.debug("before register...{}", sc.getRemoteAddress());
//round robin 轮询
workers[index.getAndIncrement() % workers.length].register(sc);
log.debug("after register...{}", sc.getRemoteAddress());
}
}
}
}
static class Worker implements Runnable {
private Thread thread;
private Selector selector;
private String name;
private volatile boolean start = false; //还未初始化
private ConcurrentLinkedQueue<Runnable> queue = new ConcurrentLinkedQueue<>();
public Worker(String name) {
this.name = name;
}
//初始化线程和selector
public void register(SocketChannel sc) throws IOException {
if (!start) {
selector = Selector.open();
thread = new Thread(this, name);
thread.start();
start = true;
}
//向队列添加了任务,但这个任务并没有立刻执行 boss
queue.add(() -> {
try {
sc.register(selector, SelectionKey.OP_READ, null);
} catch (ClosedChannelException e) {
e.printStackTrace();
}
});
selector.wakeup(); //唤醒select方法
}
@Override
public void run() {
while (true) {
try {
selector.select(); //worker-0 阻塞 wakeup
Runnable task = queue.poll();
if (task != null) {
task.run(); //执行了sc.register(selector,SelectionKey.OP_READ,null);
}
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
if (key.isReadable()) {
ByteBuffer buffer = ByteBuffer.allocate(16);
SocketChannel channel = (SocketChannel) key.channel();
log.debug("read...{}", channel.getRemoteAddress());
channel.read(buffer);
buffer.flip();
debugAll(buffer);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端代码
public class TestClient {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
sc.write(Charset.defaultCharset().encode("1234567890abcef"));
System.in.read();
}
}
首先启动服务器端
public class UdpServer {
public static void main(String[] args) {
try (DatagramChannel channel = DatagramChannel.open()) {
channel.socket().bind(new InetSocketAddress(9999));
System.out.println("waiting...");
ByteBuffer buffer = ByteBuffer.allocate(32);
channel.receive(buffer);
buffer.flip();
debug(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出
waiting...
运行客户端
public class UdpClient {
public static void main(String[] args) {
try (DatagramChannel channel = DatagramChannel.open()) {
ByteBuffer buffer = StandardCharsets.UTF_8.encode("hello");
InetSocketAddress address = new InetSocketAddress("localhost", 9999);
channel.send(buffer, address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
同步阻塞、同步非阻塞、同步多路复用、异步阻塞(没有此情况)、异步非阻塞
当调用一次channel.read或stream.read后,会切换至操作系统内核态来完成真正数据读取,而读取又分为两个阶段,分别为:
传统的IO将一个文件通过socket写出
File f = new File("helloword/data.txt");
RandomAccessFile file = new RandomAccessFile(file, "r");
byte[] buf = new byte[(int)f.length()];
file.read(buf);
Socket socket = ...;
socket.getOutputStream().write(buf);
内部的工作流程是这样的:
1.java本身并不具备IO读写能力,因此read方法调用后,要从java程序的用户态切换至内核态,去调用操作系统(Kernel)的读能力,将数据读入内核缓冲区。这期间用户线程阻塞,操作系统使用DMA(Direct Memory Access)来实现文件读,期间也不会使用cpu
DMA也可以理解为硬件单元,用来解放cpu完成文件IO
2.从内核态切换回用户态,将数据从内核缓冲区读入用户缓冲区(即byte []buf),这期间cpu会参与拷贝,无法利用DMA
3.调用write方法,这时数据从用户缓冲区(byte[] buf)写入socket缓冲区,cpu会参与拷贝
4.接下来要向网卡写数据,这项能力java又不具备,因此又得从用户态切换至内核态,调用操作系统的写能力,使用DMA将socket缓冲区的数据写入网卡,不会使用cpu
可以看到中间环节较多,java的IO实际不是物理设备级别的读写,而是缓存的赋值,底层的真正读写是操作系统来完成的
通过DirectByteBuf
进一步优化(底层采用了linux 2.1后提供的sendFile方法),java中对应着两个channel调用transferTo/transferFrom方法拷贝数据
1.java调用transferTo方法后,要从java程序的用户态切换至内核态,使用DMA将数据读入内核缓冲区,不会使用cpu
2.数据从内核缓冲区传输到socket缓冲区,cpu会参与拷贝
3.最后使用DMA将socket缓冲区的数据写入网卡,不会使用cpu
可以看到
进一步优化(linux 2.4)
1.java调用transferTo方法后,要从java程序的用户态切换至内核态,使用DMA将数据读入内核缓冲区,不会使用cpu
2.只会将一些offset和length信息拷入socket缓冲区,几乎无消耗
3.使用DMA将socket缓冲区的数据写入网卡,不会使用cpu
整个过程仅只发生了一次用户态和内核态的切换,数据拷贝了2次。所谓的【零拷贝】,并不是真正的无拷贝,而是在不会拷贝重复数据到jvm内存中,零拷贝的优点有
先来看看AsynchronousFileChannel
@Slf4j
public class AioFileChannel {
public static void main(String[] args) throws IOException {
try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("data.txt"), StandardOpenOption.READ)) {
//参数1 ByteBuffer
//参数2 读取的起始位置
//参数3 附件
//参数4 回调对象
ByteBuffer buffer = ByteBuffer.allocate(16);
log.debug("read begin...");
channel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override //read 成功
public void completed(Integer result, ByteBuffer attachment) {
log.debug("read completed...{}",result);
attachment.flip();
debugAll(attachment);
}
@Override //read 失败
public void failed(Throwable exc, ByteBuffer attachment) {
exc.printStackTrace();
}
});
log.debug("read end...");
} catch (IOException e) {
e.printStackTrace();
}
System.in.read();
}
}
默认文件AIO使用的线程都是守护线程,所以最后要执行System.in.read()以避免守护线程意外结束,守护线程在主线程结束时也会立即结束。
public class AioServer {
public static void main(String[] args) throws IOException {
AsynchronousServerSocketChannel ssc = AsynchronousServerSocketChannel.open();
ssc.bind(new InetSocketAddress(8080));
ssc.accept(null, new AcceptHandler(ssc));
System.in.read();
}
private static void closeChannel(AsynchronousSocketChannel sc) {
try {
System.out.printf("[%s] %s close\n", Thread.currentThread().getName(), sc.getRemoteAddress());
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static class ReadHandler implements CompletionHandler<Integer, ByteBuffer> {
private final AsynchronousSocketChannel sc;
public ReadHandler(AsynchronousSocketChannel sc) {
this.sc = sc;
}
@Override
public void completed(Integer result, ByteBuffer attachment) {
try {
if (result == -1) {
closeChannel(sc);
return;
}
System.out.printf("[%s] %s read\n", Thread.currentThread().getName(), sc.getRemoteAddress());
attachment.flip();
System.out.println(Charset.defaultCharset().decode(attachment));
attachment.clear();
// 处理完第一个 read 时,需要再次调用 read 方法来处理下一个 read 事件
sc.read(attachment, attachment, this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
closeChannel(sc);
exc.printStackTrace();
}
}
private static class WriteHandler implements CompletionHandler<Integer, ByteBuffer> {
private final AsynchronousSocketChannel sc;
private WriteHandler(AsynchronousSocketChannel sc) {
this.sc = sc;
}
@Override
public void completed(Integer result, ByteBuffer attachment) {
// 如果作为附件的 buffer 还有内容,需要再次 write 写出剩余内容
if (attachment.hasRemaining()) {
sc.write(attachment);
}
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
exc.printStackTrace();
closeChannel(sc);
}
}
private static class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel, Object> {
private final AsynchronousServerSocketChannel ssc;
public AcceptHandler(AsynchronousServerSocketChannel ssc) {
this.ssc = ssc;
}
@Override
public void completed(AsynchronousSocketChannel sc, Object attachment) {
try {
System.out.printf("[%s] %s connected\n", Thread.currentThread().getName(), sc.getRemoteAddress());
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer buffer = ByteBuffer.allocate(16);
// 读事件由 ReadHandler 处理
sc.read(buffer, buffer, new ReadHandler(sc));
// 写事件由 WriteHandler 处理
sc.write(Charset.defaultCharset().encode("server hello!"), ByteBuffer.allocate(16), new WriteHandler(sc));
// 处理完第一个 accpet 时,需要再次调用 accept 方法来处理下一个 accept 事件
ssc.accept(null, this);
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
}
}