程序员

高性能服务器线程模型演进 Tomcat Netty Vert.x

2020-06-16  本文已影响0人  黄大海

Hello World

ServerSocket server = new ServerSocket(8080);
    
Socket socket = server.accept();

InputStream inputStream = socket.getInputStream();

byte[] bytes = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
    sb.append(new String(bytes, 0, len,"UTF-8"));
}

System.out.println("get message from client: " + sb);

OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello World".getBytes("UTF-8"));


outputStream.close();
inputStream.close();
socket.close();
server.close();

tomcat

tomcat2.jpg

netty

netty.jpg
        final long ioStartTime = System.nanoTime();
        try {
               processSelectedKeys(); // 处理IO
        } finally {
               // Ensure we always run tasks.
               final long ioTime = System.nanoTime() - ioStartTime;
               // 用相同的时间处理回调业务
               ranTasks = runAllTasks(ioTime * (100 - ioRatio) / ioRatio);                         
        }

vert.x

vertx.jpg

为什么netty/nginx之类的高性能服务器不用AIO呢?

上一篇 下一篇

猜你喜欢

热点阅读