Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can parallelize the tasks and wait for all of them to finish before returning the call. For this, you want to use <a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html" rel="nofollow">ExecutorCompletionService</a> which is available in Java standard since 5.0</p> <p>In short, you use your container's service locator to create an instance of ExecutorCompletionService</p> <pre><code>ExecutorCompletionService&lt;List&lt;MyResult&gt;&gt; queue = new ExecutorCompletionService&lt;List&lt;MyResult&gt;&gt;(executor); // do this in a loop queue.submit(aCallable); //after looping queue.take().get(); //take will block till all threads finish </code></pre> <p>If you do not want to wait then, you can process the jobs in the background without blocking the current thread but then you will need some mechanism to inform the client when the job has finished. That can be through JMS or if you have an ajax client then, it can poll for updates.</p> <p>Quartz also has a job scheduling mechanism but, Java provides a standard way.</p> <p>EDIT: I might have misunderstood the question. If you do not want a faster response but rather you want to throttle the CPU, use this approach</p> <p>You can make an inner class like this PollingThread where batches containing java.util.UUID for each job and the number of PollingThreads are defined in the outer class. This will keep going forever and can be tuned to keep your CPUs free to handle other requests</p> <pre><code> class PollingThread implements Runnable { @SuppressWarnings("unchecked") public void run(){ Thread.currentThread().setName("MyPollingThread"); while (!Thread.interrupted()) { try { synchronized (incomingList) { if (incomingList.size() == 0) { // incoming is empty, wait for some time } else { //clear the original list = (LinkedHashSet&lt;UUID&gt;) incomingList.clone(); incomingList.clear(); } } if (list != null &amp;&amp; list.size() &gt; 0) { processJobs(list); } // Sleep for some time try { Thread.sleep(seconds * 1000); } catch (InterruptedException e) { //ignore } } catch (Throwable e) { //ignore } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload