码迷,mamicode.com
首页 > 编程语言 > 详细

使用自定义线程池优化EchoServer

时间:2018-03-04 20:02:00      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:cep   线程   通信   客户   tar   font   ddr   buffer   available   

在上一篇文章中http://www.cnblogs.com/gosaint/p/8494423.html 我自定义了线程池ThreadPool。现在在我的EchoServer中使用自定义线程池去负责和客户端的通讯,代码如下所示:

package com.asiaInfo.caozg.ch_03.threadPool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServerThread {
    private int port = 8000;
    private ServerSocket serverSocket;
    private ThreadPool threadPool;//线程池
    private static final int POOLSIZE=4;//单个CPU时的线程的数目

    public EchoServerThread() throws IOException {
        serverSocket = new ServerSocket(port);
        //创建线程池
        //Runtime.getRuntime().availableProcessors()获取当前CPU的数目
        threadPool=new ThreadPool(Runtime.getRuntime().availableProcessors()*POOLSIZE);
        System.out.println("服务器启动");
    }

    public void service() {
        while (true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();  //等待客户连接
                // 为每一个客户端创建一个线程
                threadPool.execute(new Handles(socket));//把与客户端通信的任务交给线程池
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) throws IOException {
        new EchoServerThread().service();
    }

    private class Handles implements Runnable {
        private Socket socket;

        public Handles(final Socket socket) {
            this.socket = socket;
        }

        public String echo(String msg) {
            return "echo:" + msg;
        }

        private PrintWriter getWriter(Socket socket) throws IOException {
            OutputStream socketOut = socket.getOutputStream();
            return new PrintWriter(socketOut, true);
        }

        private BufferedReader getReader(Socket socket) throws IOException {
            InputStream socketIn = socket.getInputStream();
            return new BufferedReader(new InputStreamReader(socketIn));
        }

        @Override public void run() {
            try {
                System.out.println("New connection accepted "
                        + socket.getInetAddress() + ":" + socket.getPort());
                BufferedReader br = getReader(socket);
                PrintWriter pw = getWriter(socket);
                String msg = null;
                while ((msg = br.readLine()) != null) {
                    System.out.println(msg);
                    pw.println(echo(msg));
                    if (msg.equals("bye")) //如果客户发送的消息为“bye”,就结束通信
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

}

 

 

使用自定义线程池优化EchoServer

标签:cep   线程   通信   客户   tar   font   ddr   buffer   available   

原文地址:https://www.cnblogs.com/gosaint/p/8505515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!