Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use an <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html" rel="nofollow noreferrer">ExecutorService</a> (containing multiple threads) to process your individual items of work by calling submit(). The submit method returns a Future, which will encapsulate either the result of the processing or <strong>any exception thrown</strong>. In other words, the threads within your <code>ExecutorService</code> will not terminate if an exception occurs.</p> <p><strong>Example</strong></p> <p>First create an executor service containing more than one thread:</p> <pre><code>ExecutorService execService = Executors.newFixedThreadPool(5); </code></pre> <p>Define the item of work we wish to submit as a <code>Callable</code>:</p> <pre><code>public class MyWorkItem implements Callable&lt;Integer&gt; { public Integer call() throws Exception { int result = new Random().nextInt(5); // Randomly fail. if (result == 0) { throw new IllegalArgumentException("Fail!"); } return result; } } </code></pre> <p>Submit some work for the executor service to do, and store the <code>Future&lt;Integer&gt;</code> for each <code>Callable&lt;Integer&gt;</code>.</p> <pre><code>List&lt;Future&lt;Integer&gt;&gt; futures = new LinkedList&lt;Future&lt;Integer&gt;&gt;(); for (int i=0; i&lt;10; ++i) { futures.add(execService.submit(new MyWorkItem())); } </code></pre> <p>Now iterate over the futures attempting to retrieve the result of each work item (we could use a <code>CompletionService</code> for this).</p> <pre><code>for (Future&lt;Integer&gt; future : futures) { try { Integer result = future.get(); } catch(Exception ex) { // Handle exception. } } </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.
    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