title: get query behind each executorservice thread
body: <p>I am using executorsevice in JAVA to execute some threads, let’s say ten threads, number of threads may vary. Each thread is executing a SQL server query. I am using Future and Callable classes to submit the tasks. I am getting the results [using future.get()] once each thread is finished.</p>
  
  <p>Now my requirement is that I need to know the query which is executed by each thread once its result is returned, even if the result is an empty set.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>List&lt;Future&lt;List&gt;&gt; list = new ArrayList&lt;Future&lt;List&gt;&gt;();
      int totalThreads = allQueriesWeight.size();
      ExecutorService taskExecutor = Executors.newFixedThreadPool(totalThreads);
      for (String query : allQueriesWeight) {//allQueriesWeight is an arraylist containing sql server queries
          SearchTask searchTask = new SearchTask(query);
          Future&lt;List&gt; submit = taskExecutor.submit(searchTask);
          list.add(submit);
      }
  </code></pre>
  
  <p>Here is my call function:</p>
  
  <pre><code>@Override
  public List&lt;SearchResult&gt; call() throws Exception {
      java.sql.Statement statement = null;
      Connection co = null;
      List&lt;SearchResult&gt; allSearchResults = new ArrayList();
      try {
          //executing query and getting results
          while (r1.next()) {
              ...
              allSearchResults.add(r);//populating array
          }
      } catch (Exception e) {
          Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, e);
      } finally {
          if (statement != null) {
              statement.close();
          }
          if (co != null) {
              co.close();
          }
      }
      return allSearchResults;
  }
  </code></pre>
  
  <p>Here is how I am getting the results:</p>
  
  <pre><code>for (Future&lt;List&gt; future : list) {
          try {
              System.out.println(future.get().size());
              List&lt;SearchResult&gt; sr = future.get();
          } catch (InterruptedException ex) {
              Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
          } catch (ExecutionException ex) {
              Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  </code></pre>
  
  <p>In this above for loop, I need to identify the query of which the result is returned. I am a newbie and any help/suggestion is highly appreciated.</p>
  
  <p>Thanks.</p>