Java BIO手写简易版tomcat

85 篇文章 1 订阅
28 篇文章 0 订阅

采用BIO

单线程

在这里插入图片描述

public class tomcat {
    public static final String SEPARATOR = "\r\n";
    //源码注释:requested maximum length of the queue of incoming connections.
    //BACK_LOG影响的accept队列大小
    public static final int BACK_LOG = 1024;

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        Socket socket = null;
        OutputStream outputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            serverSocket = new ServerSocket(10000, BACK_LOG);
            System.out.println("启动服务器");
            for (; ; ) {
                //阻塞式监听,会一直等待客户端连接
                socket = serverSocket.accept();
                //读取客户端消息
                outputStream = socket.getOutputStream();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
                bufferedWriter = new BufferedWriter(outputStreamWriter);
                bufferedWriter.write(HttpRequest());
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String HttpRequest() {
        String str = "<h1>tomcat</h1>";
        StringBuilder builder = new StringBuilder();
        builder.append("HTTP/1.1 200 OK").append(SEPARATOR);
        builder.append("Connection: Close").append(SEPARATOR);
        builder.append("Content-Type: text/html;charset=utf-8").append(SEPARATOR);
        builder.append("Content-Length: " + str.length()).append(SEPARATOR);
        builder.append(SEPARATOR);
        builder.append(str);
        return builder.toString();
    }
}

多线程

在这里插入图片描述

//采用BIO+线程池
public class tomcatThread {
    public static final String SEPARATOR = "\r\n";
    //源码注释:requested maximum length of the queue of incoming connections.
    //BACK_LOG影响的accept队列大小
    public static final int BACK_LOG = 1024;

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(10000, BACK_LOG);
        ThreadPoolExecutor threadPoolExecutor = buildThreadPoolExecutor();
        System.out.println("启动服务器");
        for (; ; ) {
            //阻塞式监听,会一直等待客户端连接
            Socket socket = serverSocket.accept();
            threadPoolExecutor.execute(() -> {
                OutputStream outputStream = null;
                BufferedWriter bufferedWriter = null;
                try {
                    //读取客户端消息
                    outputStream = socket.getOutputStream();
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
                    bufferedWriter = new BufferedWriter(outputStreamWriter);
                    //模拟在做事
                    Thread.sleep(500);
                    bufferedWriter.write(HttpRequest());
                    bufferedWriter.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static String HttpRequest() {
        String str = "<h1>tomcat</h1>";
        StringBuilder builder = new StringBuilder();
        builder.append("HTTP/1.1 200 OK").append(SEPARATOR);
        builder.append("Connection: Close").append(SEPARATOR);
        builder.append("Content-Type: text/html;charset=utf-8").append(SEPARATOR);
        builder.append("Content-Length: " + str.length()).append(SEPARATOR);
        builder.append(SEPARATOR);
        builder.append(str);
        return builder.toString();
    }

    //线程池
    public static ThreadPoolExecutor buildThreadPoolExecutor() {
        return new ThreadPoolExecutor(
                100,
                100,
                0,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值