Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are number of ways to do this. A better solution would be to switch and use <code>ExecutorService</code> instead of forking the threads yourself. You then can then implement <code>Callable&lt;Map&gt;</code> (instead of <code>Runnable</code>) in your <code>SecondClass</code> and return the maps that each of the jobs create.</p> <p>Something like:</p> <pre><code>// create a thread pool with as many workers as there are jobs ExecutorService threadPool = Executors.newCachedThreadPool(); List&lt;Future&lt;Map&lt;String, Integer&gt;&gt;&gt; futures = new ArrayList&lt;Future&lt;Map&lt;String, Integer&gt;&gt;&gt;(); futures.add(threadPool.submit(new SecondClass(s1))); futures.add(threadPool.submit(new SecondClass(s2))); futures.add(threadPool.submit(new SecondClass(s3))); futures.add(threadPool.submit(new SecondClass(s4))); // once we have submitted all jobs to the thread pool, it should be shutdown threadPool.shutdown(); ... Map&lt;String, Integer&gt; all = new HashMap&lt;String, Integer&gt;(); for (Future&lt;Map&lt;String, Integer&gt;&gt; future : futures) { // future.get() throws an exception if your call method throws all.putAll(future.get()); } </code></pre> <p>So then your <code>SecondClass</code> implements <code>Callable</code>:</p> <pre><code>public Map&lt;String, Integer&gt; call() { ... return map; } </code></pre> <p>Some other mechanisms that you could use include:</p> <ul> <li>Using a shared <code>ConcurrentHashMap</code> that was passed in (better) or <code>static</code> (not as good)</li> <li>Have your threads put their results on a <code>BlockingQueue</code> when they are done</li> <li>Join with the threads and then call a method on the class that gets the <code>Map</code> that was created.</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