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

  • SEARCH
  • Home
  • 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 9274575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:23:09+00:00 2026-06-18T16:23:09+00:00

I am using Executor service in one of my Multithreaded code . I am

  • 0

I am using Executor service in one of my Multithreaded code. I am trying to see whether all of my threads are finished doing their job then afterwards I need to do certain task.

Currently I need to measure the time elapsed, so I can only measure the time elapsed only after all the threads has finished executing there job. So I have this code currently? And I am measuring time elapsed in the finally block.

ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

long startTime = System.nanoTime();
try {
    for (int i = 0; i<noOfThreads; i++) {
        service.submit(new Task());
    }
    service.shutdown();
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

    //Do I need this while block here?
    while (!service.isTerminated()) {

    }
} catch (InterruptedException e) {
    //Log exception here
} finally {
    long estimatedTime = System.nanoTime() - startTime;
    logTimingInfo(estimatedTime, noOfTasks, noOfThreads);
}

I am not sure whether I need while loop here or not? Is the way I am currently doing it right or not?

Updated Code:-

So below code should work fine. Right?

ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

long startTime = System.nanoTime();
try {
    for (int i = 0; i<noOfThreads; i++) {
        service.submit(new Task());
    }
    service.shutdown();
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);     
} catch (InterruptedException e) {
    //Log exception here
} finally {
    long estimatedTime = System.nanoTime() - startTime;
}
  • 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-06-18T16:23:10+00:00Added an answer on June 18, 2026 at 4:23 pm

    Q: Do you need the while? A: No.

    The previous awaitTermination call won’t return until either the service has terminated, or (2^63 - 1) seconds have elapsed. (That is a very long time.)


    UPDATE – The updated version looks OK to me.

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

Sidebar

Related Questions

One of actions in Struts2 is using below code: java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor(); myservice.execute(new
I'm new to using threads and just trying to figure things out. My end
I want to schedule the same command in a parallel fashion using executor service
I am using a ThreadPool via ExecutorService. By calling shutDownNow() it interrupts all running
I'm using the mojo-executor library to call a number of Maven plugins from within
I am using Callable, Executor and Future objects to invoke webservices in parallel from
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Is there some known issue with this executor? Or am I using it wrong
We have 1000 threads that hit a web service and time how long the
I doubt whether java.util.concurrent.ExecutorService should be shutdown after all tasks had been completed or

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.