3

I'm building a client-server application on Java with sockets. As far as I've understood to create a thread for every client being connected is too expensive. Instead we can use ThreadPool Executor. As said in the concurrent documentation we can create a thread pool with a fixed size.

class NetworkService implements Runnable {
   private final ServerSocket serverSocket;
   private final ExecutorService pool;

   public NetworkService(int port, int poolSize)
       throws IOException {
     serverSocket = new ServerSocket(port);
     pool = Executors.newFixedThreadPool(poolSize);
   }

   public void run() { // run the service
     try {
       for (;;) {
         pool.execute(new Handler(serverSocket.accept()));
       }
     } catch (IOException ex) {
       pool.shutdown();
     }
   }
 }

And it seems we have at most poolSize thread running at every point of time. But what if we need to maintain a number of connection that is more than poolSize. How is it going to work?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

3

If you are going to have really huge amount of clients, you should consider NIO for it, because creating thread for each client will be too expensive.

NIO uses selectors and channels and doesn't require to create new thread for each connection. See image.

Did you hear about netty ? I don't know what you are going to implement but seems like it will be useful.

Maksym
  • 4,434
  • 4
  • 27
  • 46
  • Ah, Ok... I've understood... more-or-less)) – St.Antario Nov 16 '14 at 13:55
  • Well, I'm trying to implement a simple client-server massenger that can maintain around 500-1000 connection simultaneously. – St.Antario Nov 16 '14 at 15:50
  • I mean do you mean NIO would be necessary there? – St.Antario Nov 16 '14 at 15:51
  • 500-1000 it's far from huge :) nope, for your case would be enough just regular old blocking IO server , because it easier, but if your system will grow up... Author of book "Netty in Action" recommends to use NIO only for 1000+ concurrent connection. For example NIO server netty, can handle dozens or even hundreds of thousands concurrent connections. – Maksym Nov 16 '14 at 18:56