Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:57:46+00:00 2026-05-13T05:57:46+00:00

I am using the ThreadPoolExecutor to implement threading in my Java Application. I have

  • 0

I am using the ThreadPoolExecutor to implement threading in my Java Application.

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:

parse_tp is a threadpool object created & ParseQuotesXML is the class with the run method.

        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 && 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();  
        }

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 ?

Thanks,
Amit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:57:46+00:00Added an answer on May 13, 2026 at 5:57 am

    To answer your second question, I think you are doing a reasonable job trying to clean up your thread pool.

    With respect to your first question, I think the method that you want to use is submit rather than execute. 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:

    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<Integer> adapter = 
        new ThreadedListenerAdapter<Integer>(listener);
    int taskCount = 10;
    
    List<FutureTask<Integer>> taskList = new ArrayList<FutureTask<Integer>>();
    
    for (int whichTask = 0; whichTask < taskCount; whichTask++) {
        FutureTask<Integer> futureTask = 
            new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                // Does useful work that affects messagesSent
                return messagesSent;
            }
        });
        taskList.add(futureTask);
    }
    
    for (FutureTask<Integer> task : taskList) {
        LocalExecutorService.getExecutorService().submit(task);
    }
    
    for (FutureTask<Integer> 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());
    

    I think this fragment is similar to what you’re trying to do. The key components were the submit and get methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As I said in my comment the code will not… May 14, 2026 at 3:48 pm
  • Editorial Team
    Editorial Team added an answer Right click on the assembly reference, select properties, and change… May 14, 2026 at 3:48 pm
  • Editorial Team
    Editorial Team added an answer I planned on implementing Geneva (now WIF) in MOSS 2007… May 14, 2026 at 3:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.