title: Way to prioritize specific API calls with multithreads or priority queue?
body: <p>In my application, my servlet(running on tomcat) takes in a doPost request, and it returns an initial value of an api call to the user for presentation and then does a ton of data analysis in the back with a lot more other api calls. The data analysis then goes into my mongodb. The problem arises when I want to start the process before the bulk api calls are finished. There are so many calls that I would need at least 20 seconds. I don't want the user to wait for 20 seconds for their initial data display, so I want the data analysis to pause to let the new request to call for that initial api for display.</p>
  <p>Here's the general structure of my function after the doPost(async'd so this is in a Runnable). It's a bit long so I abbreviated it for easier read:</p>
  <pre><code>private void postMatches(ServletRequest req, ServletResponse res) {
          ... getting the necessary fields from the req ...
          /* read in values for arrays */
          String rankQueue = generateQueryStringArray(&quot;RankedQueues=&quot;, &quot;rankedQueues&quot;, info);
          String season = generateQueryStringArray(&quot;seasons=&quot;, &quot;seasons&quot;, info);
          String champion = generateQueryStringArray(&quot;championIds=&quot;, &quot;championIds&quot;, info);
          /* first api call, &quot;return&quot; this and then start analysis */
          JSONObject recentMatches = caller.callRiotMatchHistory(region, &quot;&quot; + playerId);
          try {
              PrintWriter out = res.getWriter();
              out.write((new Gson()).toJson(recentMatches));
              out.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          /* use array values to send more api calls */
          JSONObject matchList = caller.callRiotMatchList(playerId, region, rankQueue, season, champion);
          ... do a ton more api calls with the matchList's id's...
      }
  </code></pre>
  <p>So one of my ideas is to</p>
  <h1>Have two threads per client.</h1>
  <p>That way, there would be one thread calling that single api call, and the other thread would be calling the rest 999 api calls. This way, the single api calling thread would just wait until another doPost from the same client would come and call the api immediately, and the bulk api calls that come with it will just be appended to the other thread. By doing this, the two threads will compute in parallel.</p>
  <h1>Have a priority queue, put the initial call on high priority</h1>
  <p>This way, every URL will be passed through the queue and I can chose the compareTo of specific URL's to be greater(maybe wrap it in a bean). However, I'm not sure how the api caller will be able to distinguish which call is which, because once the url's added into the queue it loses identity. Is there any way to fix that? I know callbacks aren't available in java, so it's kind of hard to do that.</p>
  <p>Are either of these two ideas possible? No need for code, but it would be greatly appreciated!</p>
  <p>PS: I'm using Jersey for API calls.</p>