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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:45:20+00:00 2026-05-24T09:45:20+00:00

I have a singleThreadExecutor in order to execute the tasks I submit to it

  • 0

I have a singleThreadExecutor in order to execute the tasks I submit to it in serial order i.e. one task after another, no parallel execution.

I have runnable which goes something like this

MyRunnable implements Runnable {

@Override
public void run() {
    try {
        Thread.sleep(30000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

}

When I submit ,for example, three instances of MyRunnable to the afore-mentioned single thread executor, I would expect to have the first task executing and because of the Thread.sleep has Its executing thread in TIMED_WAITING (I may be wrong about the specific state). The other two tasks should not have threads assigned to execute them, at least not until the first task has finished.

So my question is how to get this state via the FutureTask API or somehow get to the thread that is executing the task (if there is no such thread then the task is waiting to be executed or pending) and get Its state or perhaps by some other means?

FutureTask only defines isCanceled() and isDone() methods, but those are not quite enough to describe all the possible execution statuses of the Task.

  • 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-24T09:45:21+00:00Added an answer on May 24, 2026 at 9:45 am

    You could add a getThread() method to MyRunnable that produces the Thread executing the run() method.

    I would suggest adding an instance variable like this (must be volatile to ensure correctness):

     private volatile Thread myThread;
    

    Do this before the try block:

    myThread = Thread.currentThread();
    

    And add a finally block with this:

    myThread = null;
    

    Then you could call:

    final Thread theThread = myRunnable.getThread();
    if (theThread != null) {
        System.out.println(theThread.getState());
    }
    

    for some MyRunnable.

    null is an ambiguous result at this point, meaning either, “hasn’t run,” or “has completed.” Simply add a method that tells whether the operation has completed:

    public boolean isDone() {
        return done;
    }
    

    Of course, you’ll need an instance variable to record this state:

    private volatile boolean done;
    

    And set it to true in the finally block (probably before setting the thread to null, there’s a bit of a race condition there because there are two values capturing the state of one thing. In particular, with this approach you could observe isDone() == true and getThread() != null. You could mitigate this by having a lock object for state transitions and synchronize on it when changing one or both state variables):

    done = true;
    

    Note that there still isn’t any guard that prohibits a single MyRunnable from being submitted concurrently to two or more threads. I know you say that you’re not doing this… today 🙂 Multiple concurrent executions will lead to corrupted state with high likelihood. You could put some mutual exclusive guard (such as simply writing synchronized on the run() method) at the beginning of the run method to ensure that only a single execution is happening at any given time.

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

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have a n-tire web application and search often times out after 30 secs. How
have Googled the cr*p out of this one so apologies if the answer is
Have a look at one of my websites: moskah.com The problem is that it
Have posting working wonderfully, and reading the response happily EXCEPT when one of the
Have a really weird one here. The app has two targets, just to be
Have any one tried to activate fancybox thumbnail gallery using a button or an
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine
Have the following scenario. I have a few form, which essentially have a few
Have a bunch of classes which I need to do serialize and deserialize from/to

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.