Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use <code>Futures</code> and <code>Callables</code> if you want to wait compute something in asynchronous and wait for the results.</p> <p>Implement <code>Callable</code> interface:</p> <pre><code>class MoveMessages implements Callable&lt;Boolean&gt; { @Override public Boolean call() throws Exception { boolean success = true; // Your implementation here return success; } </code></pre> <p>}</p> <p>And next submit it to Executor and retrieve <code>Future</code>, invoking <code>get()</code> on Future you will wait until computation of Callable is done. </p> <pre><code>ExecutorService executor = Executors.newFixedThreadPool(5); MoveMessages moveMessages = new MoveMessages(); Future&lt;Boolean&gt; submit = executor.submit(moveMessages); Boolean integer = submit.get(); // Will wait until task is finished executor.shutdown(); </code></pre> <p>Of course you can submit more task gets all to list and wait until all will finish.</p> <p><strong>Edit:</strong></p> <p>OK, first you say that you need to wait until all messages are moved, so one way for that case is to use <code>Future</code> and <code>callable</code> with <code>ExecutorService</code>. Using <code>ExecutorService</code> you do not need to create and start lots of new threads. Remember creating new thread generate costs. In your code you create 4 new threads for each sender, using <code>ExecutorService</code> you create only fixed number of threads and reuse them for each Sender. Here is your example using <code>Executors</code> and <code>Futures</code>, note that ExecutorService is created once for invoking <code>moveMessagesToFolders</code>:</p> <pre><code>private static ExecutorService executor private static void moveMessagesToFolders(List&lt;Message&gt; listMessages, Store store, Set&lt;String&gt; setSender) throws MessagingException { executor = Executors.newFixedThreadPool(4); HashMap&lt;String, List&lt;Message&gt;&gt; mapMessages = separeteMessagesBySender(listMessages, setSender); for (Map.Entry&lt;String, List&lt;Message&gt;&gt; mapMessage : mapMessages.entrySet()) { Message[] messageArray = mapMessage.getValue().toArray(new Message[mapMessage.getValue().size()]); moveMessagesThread(messageArray, mapMessage, store); } executor.shutdown(); } private static void moveMessagesThread(Message[] messageArray, Map.Entry&lt;String, List&lt;Message&gt;&gt; mapMessage, Store store) { List&lt;Future&lt;Boolean&gt;&gt; futures = createMovimentSetThread(messageArray, mapMessage, store); for (Future&lt;Boolean&gt; future : futures) { try { Boolean success = future.get(); // Will wait to accomplished all submited Callables if(!success) { // Check if all submited callables end succesulfy throw new RuntimeException("Something goes wrong while moving messages"); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } private static List&lt;Future&lt;Boolean&gt;&gt; createMovimentSetThread(Message[] messageArray, Map.Entry&lt;String, List&lt;Message&gt;&gt; mapMessage, Store store) { int [] threadIndexs = MathUtil.generateIndex(messageArray); List&lt;Future&lt;Boolean&gt;&gt; futures = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; threadIndexs.length; i++) { Future&lt;Boolean&gt; submit = executor.submit(new ThreadMoveMessages(messageArray, mapMessage, store, threadIndexs[i])); futures.add(submit); } return futures; } </code></pre> <p>According to your comments, that you split array to smaller pieces using Fork/Join framework may be a better solution. Look in google for some more information. Some links:</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html</a></li> <li><a href="https://stackoverflow.com/questions/3524634/java-7-fork-join-framework">Java 7: Fork/Join Framework</a></li> </ul>
    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