Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would recommend taking a look at <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html" rel="noreferrer">ExecutorService</a>.</p> <p>In particular, something like this:</p> <pre><code>ExecutorService EXEC = Executors.newCachedThreadPool(); List&lt;Callable&lt;Result&gt;&gt; tasks = new ArrayList&lt;Callable&lt;Result&gt;&gt;(); for (final Object object: objects) { Callable&lt;Result&gt; c = new Callable&lt;Result&gt;() { @Override public Result call() throws Exception { return compute(object); } }; tasks.add(c); } List&lt;Future&lt;Result&gt;&gt; results = EXEC.invokeAll(tasks); </code></pre> <p>Note that using <code>newCachedThreadPool</code> could be bad if <code>objects</code> is a big list. A cached thread pool could create a thread per task! You may want to use <code>newFixedThreadPool(n)</code> where n is something reasonable (like the number of cores you have, assuming <code>compute()</code> is CPU bound).</p> <p>Here's full code that actually runs:</p> <pre><code>import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ExecutorServiceExample { private static final Random PRNG = new Random(); private static class Result { private final int wait; public Result(int code) { this.wait = code; } } public static Result compute(Object obj) throws InterruptedException { int wait = PRNG.nextInt(3000); Thread.sleep(wait); return new Result(wait); } public static void main(String[] args) throws InterruptedException, ExecutionException { List&lt;Object&gt; objects = new ArrayList&lt;Object&gt;(); for (int i = 0; i &lt; 100; i++) { objects.add(new Object()); } List&lt;Callable&lt;Result&gt;&gt; tasks = new ArrayList&lt;Callable&lt;Result&gt;&gt;(); for (final Object object : objects) { Callable&lt;Result&gt; c = new Callable&lt;Result&gt;() { @Override public Result call() throws Exception { return compute(object); } }; tasks.add(c); } ExecutorService exec = Executors.newCachedThreadPool(); // some other exectuors you could try to see the different behaviours // ExecutorService exec = Executors.newFixedThreadPool(3); // ExecutorService exec = Executors.newSingleThreadExecutor(); try { long start = System.currentTimeMillis(); List&lt;Future&lt;Result&gt;&gt; results = exec.invokeAll(tasks); int sum = 0; for (Future&lt;Result&gt; fr : results) { sum += fr.get().wait; System.out.println(String.format("Task waited %d ms", fr.get().wait)); } long elapsed = System.currentTimeMillis() - start; System.out.println(String.format("Elapsed time: %d ms", elapsed)); System.out.println(String.format("... but compute tasks waited for total of %d ms; speed-up of %.2fx", sum, sum / (elapsed * 1d))); } finally { exec.shutdown(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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