title: How to make an optimized scenario for Java concurrent application?
body: <p>I've been looking for similar questions like these, but for my requirements I think I need something special, I will explain it in detail.</p>
  
  <p>First of all I need to migrate a system which used to work this way:</p>
  
  <ol>
  <li>A class called ServerPool(Thread) initializes with a main class.</li>
  <li>This ServerPool creates a queue to receive sockets and a vector to manage worker threads (S.</li>
  </ol>
  
  <p>So in code for pool, I have the following:</p>
  
  <pre><code>public class ServerPool extends Thread {
      private LinkedBlockingQueue&lt;SearchQuery&gt; workQueue; //SearchQuery is a Class I defined which can handle two type of processes (for sockets and for single Strings)
      private Vector&lt;SearchThread&gt; workers;
      private final int NTHREADS = 10;
      private int typeOfQuery;
  
      public ServerPool() {
      workers = new Vector&lt;SearchThread&gt;(NUM_THREAD);
      workQueue = new LinkedBlockingQueue&lt;SearchQuery&gt;();
      this.typeOfQuery = typeOfQuery;
      SearchThread search = new SearchThread(workQueue);
      search.start();
      workers.add(search);
  }
  
  public void run() {
      while(true){
      SearchQuery client = null;
  
      if (typeOfQuery == 1) {
          client = new SocketQuery(....);
          } else if (typeOfQuery == 2) {
          client = new StringQuery(...);
          }
          workQueue.put(client);
  }
  }
  </code></pre>
  
  <p>For the SearchThread which executes the process:</p>
  
  <pre><code>public class SearchThread extends Thread {
      private LinkedBlockingQueue&lt;SearchQuery&gt; workQueue = null;
      private SearchQuery request = null;
  
      public SearchThread(LinkedBlockingQueue&lt;SearchQuery&gt; workSource) {
          workQueue = workSource;
      }   
  
      public void run() {
          request = workQueue.take();
          //Here I process the request
          //And use a PrintWriter to give a "response"            
      }
  
  }
  </code></pre>
  
  <p>This used to work using telnet with sockets, but now I've been asked to convert it into a Web Service, so as Web Service it is supposed to return a value, so I think of using Callable, Future and Thread Pools, but I can't replicate exactly the same behavior, I tried implementing this:</p>
  
  <pre><code>public class NewServerPool {
  
      private final int NTHREADS = 10;
      private ExecutorService executor;
      private LinkedBlockingQueue&lt;SearchQuery&gt; workQueue;
      private Vector&lt;Future&lt;String&gt;&gt; futures;
      private boolean end = true;
  
      public NewServerPool(int port, SearchQuery typeOfQuery) {
          executor = Executors.newFixedThreadPool(NTHREADS);
          workQueue = new LinkedBlockingQueue&lt;SearchQuery&gt;();
          futures = new Vector&lt;Future&lt;String&gt;&gt;();
  
      }
  
  
  }
  </code></pre>
  
  <p>And for the Search Thread that now it is a Callable</p>
  
  <pre><code>public class NewSearchThread implements Callable&lt;String&gt;{
  
      private SearchQuery searchQuery;
  
      public NewSearchThread(SearchQuery searchQuery) {
          this.searchQuery = searchQuery;
      }
  
      @Override
      public String call() throws Exception {
          String xmlResponse = null;
  
          if (searchQuery == null) {
              throw new InvalidSearchQueryException("The search query is not valid or has null value: " + searchQuery);
          }
  
          if (searchQuery instanceof SocketTimed) {
              System.out.println("It is socket timed query type");
          } else if (searchQuery instanceof WebServiceQuery) {
              System.out.println("It is a web service query type");
          }
  
          xmlResponse = searchQuery.manageResponse();
  
          return xmlResponse;
      }
  </code></pre>
  
  <p>So I've got stucked in server pool, asumming my WebService will invoke a new instance of Server Pool (NewServerPool) in this case, how could I continue with this? Please I will be really grateful if somebody can help me.
  Thanks in advance, best regards.</p>