Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make the main thread wait for the other threads to complete in ThreadPoolExecutor
    primarykey
    data
    text
    <p>I am using the ThreadPoolExecutor to implement threading in my Java Application. </p> <p>I have a XML which I need to parse and add each node of it to a thread to execute the completion. My implementation is like this:</p> <p>parse_tp is a threadpool object created &amp; ParseQuotesXML is the class with the run method.</p> <pre><code> try { List children = root.getChildren(); Iterator iter = children.iterator(); //Parsing the XML while(iter.hasNext()) { Element child = (Element) iter.next(); ParseQuotesXML quote = new ParseQuotesXML(child, this); parse_tp.execute(quote); } System.out.println("Print it after all the threads have completed"); catch(Exception ex) { ex.printStackTrace(); } finally { System.out.println("Print it in the end."); if(!parse_tp.isShutdown()) { if(parse_tp.getActiveCount() == 0 &amp;&amp; parse_tp.getQueue().size() == 0 ) { parse_tp.shutdown(); } else { try { parse_tp.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException ex) { log.info("Exception while terminating the threadpool "+ex.getMessage()); ex.printStackTrace(); } } } parse_tp.shutdown(); } </code></pre> <p>The problem is, the two print out statements are printed before the other threads exit. I want to make the main thread wait for all other threads to complete. In normal Thread implementation I can do it using join() function but not getting a way to achieve the same in ThreadPool Executor. Also would like to ask if the code written in finally block to close the threadpool proper ?</p> <p>Thanks, Amit</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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