线程暂停
线程暂停
连接数过多
,必然导致 OOM,并且线程太多,反而会因为频繁上下文切换导致性能降低
阻塞简单例子:问题,当连接A建立后,1s后,A发送数据服务器收不到数据。原因就是服务器还在等待另外一个客户端的连接
阻塞模式下,一般都是一个线程对应服务一个用户
NIO下的阻塞网络编程(这并不是原生的Socket哦~)
import lombok.SneakyThrows;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
class ClientThread extends Thread {
// 0. 创建buffer
ByteBuffer buffer = ByteBuffer.allocate(16);
SocketChannel socket;
public ClientThread(SocketChannel socket) {
this.socket = socket;
}
@SneakyThrows
@Override
public void run() {
while(true) {
socket.read(buffer);
// 切换成读模式
buffer.flip();
ByteBufferUtil.debugRead(buffer);
// 切换成写模式
buffer.clear();
}
}
}
public class Server {
public static void main(String[] args) throws IOException {
// 1. 创建服务器
ServerSocketChannel ssc = ServerSocketChannel.open();
// 2. 绑定端口
ssc.bind(new InetSocketAddress(8080));
while (true) {
System.out.println("connecting...");
SocketChannel sc = ssc.accept();
new ClientThread(sc).start();
}
}
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class Client {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost", 8080));
sc.write(Charset.defaultCharset().encode("1237\\n"));
sc.write(Charset.defaultCharset().encode("1234567890abc\\n"));
System.out.println("waiting...");
System.in.read();
}
}
非阻塞简单例子:服务器端,客户端代码不变
其实代码主要就是多了:ssc.configureBlocking(false);