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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:04:34+00:00 2026-06-16T00:04:34+00:00

I am using an ExecutorService for a connection task as below: ExecutorService executor =

  • 0

I am using an ExecutorService for a connection task as below:

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<ApplicationConnection> future = (Future<ApplicationConnection>) executor.submit(new ConnectThread(crf, connoptions));
connection = future.get(300000, TimeUnit.SECONDS);
executor.shutdownNow();

The call() method calls a .connect() method (proprietary API). This connect method spawns various threadpools etc. My concern is that if the future times out and kills the executor, will the threads that may have already spawned by calling the .connect() method in the future also end? I know that killing a thread will also kill any child threads but does this follow the same logic?

  • 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-16T00:04:35+00:00Added an answer on June 16, 2026 at 12:04 am

    You are right in your assumption, if the Future times out, some hanging threads will remain. Even worse, shutdownNow() will not even shutdown your pool thread (not to mention proprietary API threads). It merely stops accepting new jobs. ExecutorService thread pool will terminate all threads once all running tasks finish.

    What you can do is to try canceling the future and interrupting it. First handle InterruptedException inside your future:

    class ConnectThread implements Callbale<ApplicationConnection> {
    
        public ApplicationConnection call() {
            try {
                return prioprietaryApi.connect();
            } catch(InterruptedException e) {
                prioprietaryApi.cleanUp();
                throw e;
            }
        }
    
    }
    

    Now simply run:

    future.cancel(true);
    

    However your proprietary API might not handle InterruptedException (it will not rethrow it from connect(), moreover you might not have access to any cleanUp() method.

    In these circumstances just… forget about it. That Future will eventually terminate and clean up after itself, ignoring the fact that you no longer wait for it. Of course this might lead to various scalability issues.

    BTW if the only thing you want to achieve is limiting the maximum time given method runs, consider TimeLimiter from guava.

    • 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 using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
When using the ExecutorService returned by Executors.newSingleThreadExecutor() , how do I interrupt it?
say I'm using a ExecutorService ex = Executors.newFixedThreadPool(nrofthreads); spinning up some work and waiting
I am trying to implement multithreading using ExecutorService for downloading files parallely. Below is
I am using java.util.concurrent.ExecutorService.submit(Callable<T>) to invoke Callable tasks that return void. The reason that
I am using Executors.newSingleThreadExecutor() in my code. I want to monitor number of tasks
I was using an ExecutorService to schedule tasks to be executed in future. After
I'm using a ConcurrentLinkedQueue to store computational steps, and an ExecutorService created by Executors.newFixedThreadPool
I submit some some Future tasks using a CompletionService wrapped round a 2 thread

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.