Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer your second question, I think you are doing a reasonable job trying to clean up your thread pool. </p> <p>With respect to your first question, I think the method that you want to use is <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/AbstractExecutorService.html#submit%28java.util.concurrent.Callable%29" rel="nofollow noreferrer">submit</a> rather than <code>execute</code>. Rather than try to explain it all in text, here's an edited fragment from a unit test that I wrote that makes many tasks, has each of them do a fragment of the total work and then meets back at the starting point to add the results:</p> <pre><code>final AtomicInteger messagesReceived = new AtomicInteger(0); // ThreadedListenerAdapter is the class that I'm testing // It's not germane to the question other than as a target for a thread pool. final ThreadedListenerAdapter&lt;Integer&gt; adapter = new ThreadedListenerAdapter&lt;Integer&gt;(listener); int taskCount = 10; List&lt;FutureTask&lt;Integer&gt;&gt; taskList = new ArrayList&lt;FutureTask&lt;Integer&gt;&gt;(); for (int whichTask = 0; whichTask &lt; taskCount; whichTask++) { FutureTask&lt;Integer&gt; futureTask = new FutureTask&lt;Integer&gt;(new Callable&lt;Integer&gt;() { @Override public Integer call() throws Exception { // Does useful work that affects messagesSent return messagesSent; } }); taskList.add(futureTask); } for (FutureTask&lt;Integer&gt; task : taskList) { LocalExecutorService.getExecutorService().submit(task); } for (FutureTask&lt;Integer&gt; task : taskList) { int result = 0; try { result = task.get(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } catch (ExecutionException ex) { throw new RuntimeException("ExecutionException in task " + task, ex); } assertEquals(maxMessages, result); } int messagesSent = taskCount * maxMessages; assertEquals(messagesSent, messagesReceived.intValue()); </code></pre> <p>I think this fragment is similar to what you're trying to do. The key components were the <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/AbstractExecutorService.html#submit%28java.util.concurrent.Callable%29" rel="nofollow noreferrer">submit</a> and <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/FutureTask.html#get%28%29" rel="nofollow noreferrer">get</a> methods.</p>
    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