Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed the NCE/CS to be clearer (similar to previous ones)

...

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

final class GetRequestRequestHandler {
  private final Helper h = new Helper();
  private static StringServerSocket requestserver;
  
  public synchronizedstatic Stringvoid acceptcreateServer(int port) throws IOException {
    String dataserver = "Read data from pipe"new ServerSocket(port);
    // Read the request data, else block}
    return data;
  }

  public void handleRequest() {
    new whileThread(new Runnable(true) {
      requestpublic =void acceptrun(); {
      new Thread(new Runnable() try {
          public void run(h.handle(server.accept());
	} catch (IOException e) {
 	  // Forward to handler    h.handle(request);
        }
      }
    }).start();
    }
  }
}

Compliant Solution

Wiki Markup
This compliant solution uses a _Fixed Thread Pool_ that places an upper bound on the number of simultaneously executing threads. Tasks submitted to the pool are stored in an internal queue. This prevents the system from getting overwhelmed when trying to respond to all incoming requests and allows it to degrade gracefully by serving a fixed number of clients at a particular time. \[[Tutorials 08|AA. Java References#Tutorials 08]\]

Code Block
bgColor#ccccff
final class GetRequestRequestHandler {
  private final Helper h = new Helper();
  String requestprivate static ServerSocket server;
  private  final int NoOfThreads = 200; // Maximum number of threads allowed in pool
  final Executor exec;
 
  GetRequest() {static ExecutorService exec;
	 
  public static void createServer(int port, int poolSize) throws IOException {
    server = new ServerSocket(port);
    exec = (Executor) Executors.newFixedThreadPool(NoOfThreadspoolSize);  	    
  }
	  
  public synchronizedvoid String accepthandleRequest() {
  	  String data = "Read data from pipe";
    // Read the request data, else blockexec.submit(new Runnable() {
    return data;
  }

  public void handleRequestrun() {
    while(true) 	try {
  	    request = h.handle(server.accept());
	} catch     exec.execute(new Runnable((IOException e) {
        public void run() {
  // Forward to handler						
        h.handle(request);}
        }
      });
    }
  }
}

Wiki Markup
According to the Java API \[[API 06|AA. Java References#API 06]\] documentation for the {{Executor}} interface:

Wiki Markup
\[The Interface {{Executor}} is\] An object that executes submitted {{Runnable}} tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An {{Executor}} is normally used instead of explicitly creating threads.

The interface ExecutorInterface used in this compliant solution derives from the Executor interface and allows callers to also obtain a "future" (result of an asynchronous computation). The caller can use the future to perform additional tasks such as task cancellation.

The choice of the unbounded newFixedThreadPool may not always be the best. Refer to the API documentation for choosing between newFixedThreadPool, newCachedThreadPool, newSingleThreadExecutor and newScheduledThreadPool to meet the design requirements.

...