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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:05+00:00 2026-05-17T23:57:05+00:00

I have implemented custom cancellation logic as described in Concurrency in Practice. Encapsulating nonstandard

  • 0

I have implemented custom cancellation logic as described in Concurrency in Practice.

Encapsulating nonstandard cancellation in a task with newTaskFor.

This works fine and I can call cancel on the a future and the task is cancelled as expected. I need to be able to destroy my executor service which i am using by calling “shutdownNow” method, but this method simply calls interupt on the threads which means my custom cancel logic is never called. As my tasks use non-blocking sockets interrupting the thread does not work hence why i have custom cancellation logic.

Is there a simple solution to cancel all the tasks in progress. I have tried overwriting the shutdown method on ThreadPoolExecutor but I don’t have access to the list of workers. I would like to be able to cancel everything by shutting down the executor as its used in several places to submit tasks, is there a simple solution to this?

  • 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-17T23:57:05+00:00Added an answer on May 17, 2026 at 11:57 pm

    As John pointed out, it would be best if you can make your tasks interruptible. Then you can simply rely on the ThreadPoolExecutor to interrupt all worker threads to have an orderly cancellation.

    If that is not possible, you can add additional logic in ThreadPoolExecutor to achieve what you want. It is somewhat involved, and might not be pretty to some (and may hurt performance a bit), but I think it’ll get the job done. I think basically you need to maintain a list of active tasks yourself. The key is to override the beforeExecute() and the afterExecute() methods:

    public class MyExecutor extends ThreadPoolExecutor {
        private final Queue<RunnableFuture> activeTasks = 
                new LinkedBlockingQueue<RunnableFuture>();
        ...
    
        protected void beforeExecute(Thread t, Runnable r) {
            RunnableFuture task = (RunnableFuture)r;
            activeTasks.add(task);
        }
    
        protected void afterExecute(Thread t, Runnable r) {
            RunnableFuture task = (RunnableFuture)r;
            activeTasks.remove(task);
        }
    
        public void cancelAllActiveTasks() {
            for (RunnableFuture f: activeTasks) {
                f.cancel(true); // invokes your custom cancellation logic
            }
        }
    

    You can call cancelAllActiveTasks() or override shutDownNow() to call it as well. One thing I do not like about this is having to remove the task from the queue as it’s not a constant-time operation.

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

Sidebar

Related Questions

I have implemented custom errors in my asp.net mvc application by following this article
I have implemented the Custom Textbox in my app and it works well but
I have implemented a custom ListItem for my ListView. The ListItem looks like this:
I have implemented custom class representing an enumeration possibilities similarily to this article :
I have implemented a custom UINavigationbar using this code (placed in the top of
I have implemented a custom split view controller which — in principle — works
I have implemented a custom ActionMapper which obtains the locale from the URI (the
I have implemented a custom view within another activity into a XML layout. The
In my WCF service I have implemented a custom encoder which inherits from System.ServiceModel.Channels.MessageEncoder
I'm using OSMdroid to implement a mapping application. I have implemented a custom MapTileProvider

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.