使用ExecutorService的线程池

时间:2014-02-14 22:45:28

标签: java multithreading concurrency threadpool executorservice

我正在创建一个聊天客户端,它通过与服务器通信来进行大约500,000个用户注册。客户端向服务器发送请求,然后在处理请求后将响应发送回客户端。我使用Iterative Server和MultiThreaded服务器实现了这一点,该服务器使用Java中的ExecutorService框架。我有一个大小为10的固定线程池。我现在正在尝试测量迭代和多线程案例中每秒从服务器返回的平均注册响应数。我观察到,与迭代相比,我从多线程获得的响应更少。这没有意义,因为我有10个线程可以同时处理多线程服务器上的请求。我不确定我是否编程错误,或者我是否误解了这里的概念。我将不胜感激。

编辑:我尝试将pool.execute(new SpawnConnection(client));替换为Thread thread = new Thread(new SpawnConnection(client)); thread.start();,每秒的响应次数进一步减少。

    public class MultiThreadedServer {
        public static final int THREAD_COUNT = 10;
        private static ExecutorService pool = Executors.newFixedThreadPool(THREAD_COUNT);
        private static HashMap<String,Chat> chatGroup = new HashMap<String,Chat>();
    private static HashMap<String,Long> heartbeatChecker = new HashMap<String,Long>();

public static synchronized void putHeartbeat(String user){
        heartbeatChecker.put(user, System.currentTimeMillis());
    }

    public static synchronized void putGroup(String user, Chat c){
        chatGroup.put(user,c);
    }

        public static void main(String [] args){

            ServerSocket server = null;
            Socket client;
            int port = Integer.parseInt(args[0]);
            try{
                server = new ServerSocket(port);
            }catch (IOException e1){
                e1.printStackTrace();
                System.exit(1);
            }

            while(true){
                try {
                    client = server.accept();
                    pool.execute(new SpawnConnection(client));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

这是SpawnConnectionClass:

public class SpawnConnection implements Runnable{
    private Socket client;
    public SpawnConnection(Socket client){
        this.client = client;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter writer = new PrintWriter(client.getOutputStream(),true);
            String line = reader.readLine();
            if(line.startsWith("register ")){
                if(register(line)){
                    writer.println("Registration Success");
                }
                else{
                    writer.println("Error on Registration");
                }
            }
            else{
                writer.println("Invalid request");
            }
            client.close();
        }catch(IOException e){

        }

    }


    private boolean register(String line){
        String params[] = line.split(" ");

        if(MultiThreadedServer.groupContains(params[1])){
            return false;//writer.println("Error on register: username already exists. please try again.");
        }
        else{
            try {
                MultiThreadedServer.putGroup(params[1], new Chat(params[2],Integer.parseInt(params[3])));
                MultiThreadedServer.putHeartbeat(params[1]);
            }catch(Exception e){
                return false;
            }
            return true;
        }
    }

}

0 个答案:

没有答案