bio模型

作者: 程序男保姆 | 来源:发表于2020-05-05 01:49 被阅读0次
  • 优点

模型简单 编码简单

  • 缺点

性能瓶颈 请求数和线程数1:1的关系。高并发情况下,CPU切换线程上下文损耗大

  • 案例

web服务器tomcat7之前都是bio,7之后使用的是bio

  • 改进

使用线程池接受 socket

  • bio模型


    bio模型
  • 重点类

ServerSocket , Socket = server.accept()

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class Server {

    public static ExecutorService executorService =
            new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));

    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            ServerHandler serverHandler= new ServerHandler();

            server = new ServerSocket(8888);

            while (true){
                Socket accept = server.accept();
                executorService.execute(() -> serverHandler.run(accept));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //一些必要的清理工作
            if(server != null){
                System.out.println("服务器已关闭。");
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

public class ServerHandler {

    public void run(Socket socket) {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            out = new PrintWriter(socket.getOutputStream(), true);

            String body = null;

            while ((body = in.readLine()) != null && body.length() != 0) {
                System.out.println("接受到的消息:" + body);
                out.println(new Date());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

import java.io.*;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        send("1234");
    }

    public static void send(String expression){
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try{
            socket = new Socket( "127.0.0.1",8888);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            out.println(expression);
            System.out.println("___结果为:" + in.readLine());
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //一下必要的清理工作
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                out.close();
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

相关文章

  • Netty-理解selector是什么

    BIO的弊端 BIO既是Blocking IO,也叫同步阻塞模型,BIO模型如下 如果所示,多个客户端连接一个服务...

  • I/O-BIO

    BIO by shihang.mai BIO:ScoketIO 阻塞IO模型,举例:SocketIOPropert...

  • BIO、NIO、AIO的理解

    一、BIO的理解 首先我们通过通信模型图来熟悉下BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立...

  • 一张图解决NIO原理

    BIO 阻塞式IO BIO模型 我们可以看到BIO模型中有两处阻塞的地方,第一是socket一直等待请求的到来,第...

  • Netty学习内容

    java中的几种I/O模型 BIO通信模型 BIO通信模型图 网络编程的基本模型是C/S模型,两个进程之间进行通信...

  • bio模型

    优点 模型简单 编码简单 缺点 性能瓶颈 请求数和线程数1:1的关系。高并发情况下,CPU切换线程上下文损耗大 案...

  • Nginx高性能原因

    nginx高性能原因 epoll多路复用 非阻塞IO操作 java bio模型,阻塞进程式java bio模型 l...

  • 0、常见的几种IO模型

    1、操作系统内核 2、BIO模型 BIO(blocking I/O)是阻塞IO模型,每个客户端连接上之后都会启用一...

  • Java 开发必备! I/O与Netty原理精讲

    一 Java I/O模型 1 BIO(Blocking IO) BIO是同步阻塞模型,一个客户端连接对应一个处理线...

  • 基于线程模型的 Tomcat 参数调优

    Tomcat 中,BIO、NIO 是基于主从 Reactor 线程模型实现的。 在 BIO 中,Tomcat 中的...

网友评论

      本文标题:bio模型

      本文链接:https://www.haomeiwen.com/subject/vacpghtx.html