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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:39:42+00:00 2026-05-28T05:39:42+00:00

In a web controller, I have a parent thread that receives requests. Some requests

  • 0

In a web controller, I have a parent thread that receives requests. Some requests take a long time to process. To prevent clients from timing out, I set up the parent thread to send back a byte every 2 seconds while a child thread is doing the time-consuming part of the operation.

I want to make sure I’m accounting for all possible cases of the child thread dying, but I also don’t want to put in any extraneous checks.

Here is the parent thread:

    // This is my runnable class
    ProcessorRunnable runnable = new ProcessorRunnable(settings, Thread.currentThread());
    Thread childThread = new Thread(runnable);
    childThread.start();

    boolean interrupted = false;
    while (!runnable.done) { // <-- Check in question
        outputStream.write(' ');
        outputStream.flush();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // If the runnable is done, then this was an expected interrupt
            // Otherwise, remember the interruption and re-interrupt after processing is done
            // Or with self so that a later expected interrupt won't clear out an earlier unexpected one
            interrupted = interrupted || !runnable.done;
        }
    }
    if (runnable.runtimeException != null) {
        LOG.error("Propagating runtime exception from thread");
        throw runnable.runtimeException;
    }

    // ... Further processing on the results provided by the child thread

And here’s ProcessorRunnable:

    private volatile boolean done;
    private volatile Result result;
    private volatile RuntimeException runtimeException;

    // ...

    public void run() {
        done = false;
        try {
            result = myService.timeConsumingOperation(settings);
        } catch (RuntimeException e) {
            runtimeException = e;
        } finally {
            done = true;
            parentThread.interrupt();
        }
    }

My question is, would adding && Thread.isAlive() check in the parent thread’s main loop buy me anything?
It seems that setting done = true in the finally block should do the trick, but are there some cases where this child thread could die without notifying the parent?

  • 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-28T05:39:43+00:00Added an answer on May 28, 2026 at 5:39 am

    The finally in the child thread will always execute before it finishes. Even if that thread is interrupted or stopped, this happens via an exception that bubbles up the call stack and triggers all finallys. So, done will always be true if the child thread is interrupted.

    For background tasks like this you may want to use an ExecutorService instead of raw threads. You can submit a Runnable to an ExecutorService and just call get() on the returned future to block until it is done. If you want to print out spaces while you are waiting, you can use a loop, calling the get() version with a timeout.

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

Sidebar

Related Questions

I have an ASP.NET MVC2 application that uses a parent controller to setup specific
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I have a UIViewController and in that controller, i am fetching an image from
I have standard servlet-mapping for Dispatcher Servlet - /app/* . I have controller that
I have to store messages that my web app fetch from Twitter into a
I have a Spring 3.0 application, with an Web Controller Method. This method normaly
I have a Web Browser Controller embedded in an MFC application (using IE9 underneath).
I am using ASP.NET MVC 2, .NET 4.0. I have a controller that disables
I've currently got a BaseController class that inherits from System.Web.Mvc.Controller . On that class
I have a web application developed with Spring 2.5. The application has numerous controllers,

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.