Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
class Helper {
  public void handle(Socket socket) {
    //... 		
  }	
}

final class RequestHandler {
  private final Helper h = new Helper();
  private staticfinal ServerSocket server;

  private RequestHandler(int port) throws IOException {
    server = new ServerSocket(port);
  }
  
  public static RequestHandler getInstance(int port) throws IOException {
    return new RequestHandler(port);
  }
  
  public void handleRequest() {
    new Thread(new Runnable() {
      public void run() {
        try {
          h.handle(server.accept());
	} catch (IOException e) {
 	  // Forward to handler   
        }
      }
    }).start();
  }
}

...

Code Block
bgColor#ccccff
// class Helper remains unchanged

final class RequestHandler {
  private final Helper h = new Helper();
  private staticfinal ServerSocket server;
  private staticfinal ExecutorService exec;
	 
  private RequestHandler(int port, int poolSize) throws IOException {
    server = new ServerSocket(port);
    exec = Executors.newFixedThreadPool(poolSize);
  }
	  
  public static RequestHandler getInstance(int port, int poolSize) throws IOException {
    return new RequestHandler(port, poolSize);
  }
	  
  public void handleRequest() {	        
    exec.submit(new Runnable() {
      public void run() {
	try {
  	  h.handle(server.accept());
	} catch (IOException e) {
          // Forward to handler						
        }
      }
    });
  }
}

...