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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:42:56+00:00 2026-06-12T22:42:56+00:00

So I asked a question a little while ago: Over here that asked the

  • 0

So I asked a question a little while ago: Over here that asked the question “How can I make my threads get killed if they take too long”

I have implemented the solution mentioned there but under certain rare circumstances where a thread times out, the program can still fail / lock up (see: Keep the main() method open, and prevent further cron runs of the program).

Here’s the source I’m using:

 //Iterate through the array to submit them into individual running threads.
    ExecutorService threadPool = Executors.newFixedThreadPool(12);
    List<Future<?>> taskList = new ArrayList<Future<?>>();
    for (int i = 0; i < objectArray.length; i++) {
        Future<?> task = threadPool.submit(new ThreadHandler(objectArray[i], i));
        taskList.add(task);
        Thread.sleep(500);
    }

    //Event handler to kill any threads that are running for more than 30 seconds (most threads should only need .25 - 1 second to complete.
    for(Future future : taskList){
        try{
            future.get(30, TimeUnit.SECONDS);
        }catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
        }catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
        }catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
        }catch(TimeoutException ex) {future.cancel(true);}
    }
    threadPool.shutdown();
    threadPool.awaitTermination(60, TimeUnit.SECONDS);

So my question is: with this code implemented, why is the executor service not cutting things off at 30 seconds.

  • 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-12T22:42:57+00:00Added an answer on June 12, 2026 at 10:42 pm

    Because I suspect your worker threads are still running. You are calling future.cancel(true); but all that does is set the interrupt flag on the thread — it does not actively interrupt your running code. Another way to “interrupt” code is to set some volatile boolean shutdown flag to be true and to test for that in your code. See here for more details about interrupting threads.

    You need to make sure that your ThreadHandler code handles interrupts correctly. It needs to, for example, check Thread.currentThread().isInterrupted() in loops or in other code blocks. You also need to make sure that you are handling InterruptedException correctly and not just swallowing the interrupt.

    See my answer here for more information about thread interrupts.

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

Sidebar

Related Questions

This question is related to this initial question asked a little while ago. Now,
I know that this question have been asked several times. But I can't get
I'm just rephrasing the question I asked a little while ago. I have a
I asked a little while ago a question about rollback, I have a new
a while ago i asked this question Jquery tabs keep tab open that is
This question is related to this question I asked a little while back. The
I asked this question a while ago. I now know it is a Bad
I asked a question yesterday about a little app that I would like to
I know this question may be a little bit common and over asked but
This is a little different than the questions that have already been asked on

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.