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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:15:16+00:00 2026-05-26T21:15:16+00:00

I am using an ExecutorService (a ThreadPoolExecutor) to run (and queue) a lot of

  • 0

I am using an ExecutorService (a ThreadPoolExecutor) to run (and queue) a lot of tasks. I am attempting to write some shut down code that is as graceful as possible.

ExecutorService has two ways of shutting down:

  1. I can call ExecutorService.shutdown() and then ExecutorService.awaitTermination(...).
  2. I can call ExecutorService.shutdownNow().

According to the JavaDoc, the shutdown command:

Initiates an orderly shutdown in which previously submitted
tasks are executed, but no new tasks will be accepted.

And the shutdownNow command:

Attempts to stop all actively executing tasks, halts the
processing of waiting tasks, and returns a list of the tasks that were
awaiting execution.

I want something in between these two options.

I want to call a command that:
  a. Completes the currently active task or tasks (like shutdown).
  b. Halts the processing of waiting tasks (like shutdownNow).

For example: suppose I have a ThreadPoolExecutor with 3 threads. It currently has 50 tasks in the queue with the first 3 actively running. I want to allow those 3 active tasks to complete but I do not want the remaining 47 tasks to start.

I believe I can shutdown the ExecutorService this way by keeping a list of Future objects around and then calling cancel on all of them. But since tasks are being submitted to this ExecutorService from multiple threads, there would not be a clean way to do this.

I’m really hoping I’m missing something obvious or that there’s a way to do it cleanly.

Thanks for any help.

  • 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-26T21:15:16+00:00Added an answer on May 26, 2026 at 9:15 pm

    I ran into this issue recently. There may be a more elegant approach, but my solution is to first call shutdown(), then pull out the BlockingQueue being used by the ThreadPoolExecutor and call clear() on it (or else drain it to another Collection for storage). Finally, calling awaitTermination() allows the thread pool to finish what’s currently on its plate.

    For example:

    public static void shutdownPool(boolean awaitTermination) throws InterruptedException {
    
        //call shutdown to prevent new tasks from being submitted
        executor.shutdown();
    
        //get a reference to the Queue
        final BlockingQueue<Runnable> blockingQueue = executor.getQueue();
    
        //clear the Queue
        blockingQueue.clear();
        //or else copy its contents here with a while loop and remove()
    
        //wait for active tasks to be completed
        if (awaitTermination) {
            executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
        }
    }
    

    This method would be implemented in the directing class wrapping the ThreadPoolExecutor with the reference executor.

    It’s important to note the following from the ThreadPoolExecutor.getQueue() javadoc:

    Access to the task queue is intended primarily for debugging and
    monitoring. This queue may be in active use. Retrieving the task queue
    does not prevent queued tasks from executing.

    This highlights the fact that additional tasks may be polled from the BlockingQueue while you drain it. However, all BlockingQueue implementations are thread-safe according to that interface’s documentation, so this shouldn’t cause problems.

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

Sidebar

Related Questions

I'm firing off tasks using an ExecutorService, dispatching tasks that need to be grouped
I wrote an application that runs some threads using ExecutorService and waits until they
say I'm using a ExecutorService ex = Executors.newFixedThreadPool(nrofthreads); spinning up some work and waiting
Using Nunit, I want to be able to write a test fixture that will
I am using ExecutorService to run parallel junit tests. Each test uses a random
I'm using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
I'm using an ExecutorService to execute some Callables, but the name of the threads
One of actions in Struts2 is using below code: java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor(); myservice.execute(new
I submit some some Future tasks using a CompletionService wrapped round a 2 thread
I am using Executors.newSingleThreadExecutor() in my code. I want to monitor number of tasks

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.