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 6476219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:47:53+00:00 2026-05-25T06:47:53+00:00

I have a ThreadPoolExecutor that seems to be lying to me when I call

  • 0

I have a ThreadPoolExecutor that seems to be lying to me when I call getActiveCount(). I haven’t done a lot of multithreaded programming however, so perhaps I’m doing something incorrectly.

Here’s my TPE

@Override
public void afterPropertiesSet() throws Exception {

    BlockingQueue<Runnable> workQueue;
    int maxQueueLength = threadPoolConfiguration.getMaximumQueueLength();
    if (maxQueueLength == 0) {
        workQueue = new LinkedBlockingQueue<Runnable>();
    } else {
        workQueue = new LinkedBlockingQueue<Runnable>(maxQueueLength);
    }

    pool = new ThreadPoolExecutor(
                   threadPoolConfiguration.getCorePoolSize(),
                   threadPoolConfiguration.getMaximumPoolSize(),
                   threadPoolConfiguration.getKeepAliveTime(),
                   TimeUnit.valueOf(threadPoolConfiguration.getTimeUnit()),
                   workQueue,
                   // Default thread factory creates normal-priority,
                   // non-daemon threads.
                   Executors.defaultThreadFactory(),
                   // Run any rejected task directly in the calling thread.
                   // In this way no records will be lost due to rejection
                   // however, no records will be added to the workQueue
                   // while the calling thread is processing a Task, so set
                   // your queue-size appropriately.
                   //
                   // This also means MaxThreadCount+1 tasks may run
                   // concurrently. If you REALLY want a max of MaxThreadCount
                   // threads don't use this.
                   new ThreadPoolExecutor.CallerRunsPolicy());
}

In this class I also have a DAO that I pass into my Runnable (FooWorker), like so:

@Override
public void addTask(FooRecord record) {
    if (pool == null) {
        throw new FooException(ERROR_THREAD_POOL_CONFIGURATION_NOT_SET);
    }
    pool.execute(new FooWorker(context, calculator, dao, record));
}

FooWorker runs record (the only non-singleton) through a state machine via calculator then sends the transitions to the database via dao, like so:

public void run() {
    calculator.calculate(record);
    dao.save(record);
}

Once my main thread is done creating new tasks I try and wait to make sure all threads finished successfully:

while (pool.getActiveCount() > 0) {
    recordHandler.awaitTermination(terminationTimeout, 
                                   terminationTimeoutUnit);
}

What I’m seeing from output logs (which are presumably unreliable due to the threading) is that getActiveCount() is returning zero too early, and the while() loop is exiting while my last threads are still printing output from calculator.

Note I’ve also tried calling pool.shutdown() then using awaitTermination but then the next time my job runs the pool is still shut down.

My only guess is that inside a thread, when I send data into the dao (since it’s a singleton created by Spring in the main thread…), java is considering the thread inactive since (I assume) it’s processing in/waiting on the main thread.

Intuitively, based only on what I’m seeing, that’s my guess. But… Is that really what’s happening? Is there a way to “do it right” without putting a manual incremented variable at the top of run() and a decremented at the end to track the number of threads?

If the answer is “don’t pass in the dao”, then wouldn’t I have to “new” a DAO for every thread? My process is already a (beautiful, efficient) beast, but that would really suck.

  • 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-25T06:47:54+00:00Added an answer on May 25, 2026 at 6:47 am

    As the JavaDoc of getActiveCount states, it’s an approximate value: you should not base any major business logic decisions on this.

    If you want to wait for all scheduled tasks to complete, then you should simply use

    pool.shutdown();
    pool.awaitTermination(terminationTimeout, terminationTimeoutUnit);
    

    If you need to wait for a specific task to finish, you should use submit() instead of execute() and then check the Future object for completion (either using isDone() if you want to do it non-blocking or by simply calling get() which blocks until the task is done).

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

Sidebar

Related Questions

Is there a join()-like method for threads that have been executed through the ThreadPoolExecutor?
I have a ThreadPoolExecutor that is constructed with an unbounded queue (LinkedBlockingQueue) and a
Have had to write my first proper multithreaded coded recently, and realised just how
I have a procedure (procA) which needs to call a webservice many times and
I have a Runnable running inside a ThreadPoolExecutor long polling on an http request
I'm experimenting with some multithreading constructions, but somehow it seems that multithreading is not
I noticed that my java application (runs on tomcat6) spawns a lot of threads
I am using an ExecutorService (a ThreadPoolExecutor) to run (and queue) a lot of
I have a block of images that I want to load on my screen.
I'm writing a message processing application (email) that I want to have an outgoing

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.