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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:08:23+00:00 2026-06-16T22:08:23+00:00

I have this frustrating issue: I am trying to see if TimeoutException is thrown

  • 0

I have this frustrating issue: I am trying to see if TimeoutException is thrown in below code, but that doesn’t happen ever. It always prints the current time 5 times and finishes.

class MyTask1 implements Callable<Void> {  

    public Void call() throws Exception {  
        for (int i=0; i < 5; i++) {
            System.out.println(new java.util.Date());
            Thread.sleep(1000);
        }
        return null;
    }  
}

public class Timeout {
    public static void main(String[] args) throws Exception {
        FutureTask<Void> futureTask = new FutureTask<Void>(new MyTask1());
        futureTask.run();
        try {
            futureTask.get(1000, TimeUnit.MILLISECONDS); // Check if FutureTask completed in the given time
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            futureTask.cancel(true);
            e.printStackTrace();
        }   
    }
}

I am using Thread.sleep(1000) so that call() method takes at least 5 seconds (as it is done 5 times using a for loop). And I am calling get() method by setting timeout value 1 second.

But why doesn’t it throw TimeoutException?

This could be a possible duplicate of How do I get FutureTask to return after TimeoutException?

But appriciate if someone can help as this is really frustrating. Or I didn’t understand how to use the FutureTask to handle the timeout?

  • 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-16T22:08:24+00:00Added an answer on June 16, 2026 at 10:08 pm

    You need to run the FutureTask on a separate thread (using, e.g. an Executor). you are currently running it in the same thread, so the get() call doesn’t happen until the run() has already completed.

    UPDATE:

    If you need help with threading in java, there is a fantastic tutorial in the oracle docs.

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

Sidebar

Related Questions

I am having a rather frustrating issue trying to call a web service that
I realise that there are many existing questions regarding this issue, but I haven't
This is becoming a very frustrating issue. I am trying to do a clean
I'm well aware that variants of this question have been asked before, but I
I have one small, but frustrating issue. I create installer with possibility for shortcut
I have a frustrating issue when trying to pass a custom object from 1
Having a very frustrating time solving this issue. I have a model which automatically
This is a really frustrating problem (bug?) that I've been trying to figure out.
I tried endlessly now you resolve this utter frustrating situation I have been having.
This is so frustrating! I don't know why this is happening. I have a

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.