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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:41:49+00:00 2026-05-15T15:41:49+00:00

I am using the CountDownLatch to synchronize an initialization process between two threads and

  • 0

I am using the CountDownLatch to synchronize an initialization process between two threads and I was wondering about the proper handling of the InterruptedException that it might throw.

the code i initially wrote was this:

    private CountDownLatch initWaitHandle = new CountDownLatch(1);
    /**
     * This method will block until the thread has fully initialized, this should only be called from different threads  Ensure that the thread has started before this is called.
     */
    public void ensureInitialized()
    {
        assert this.isAlive() : "The thread should be started before calling this method.";
        assert Thread.currentThread() != this, "This should be called from a different thread (potential deadlock)";
        while(true)
        {
            try
            {
                //we wait until the updater thread initializes the cache
                //that way we know 
                initWaitHandle.await();
                break;//if we get here the latch is zero and we are done
            } 
            catch (InterruptedException e)
            {
                LOG.warn("Thread interrupted", e);
            }
        }
    }

Does this pattern make sense? Basically is it a good idea to ignore the InterruptedException just keep waiting until it succeeds. I guess I just don’t understand the situations under which this would get interrupted so I don’t know if I should be handling them differently.

Why would an InterruptedException get thrown here, what is the best practice for handling it?

  • 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-15T15:41:49+00:00Added an answer on May 15, 2026 at 3:41 pm

    This is exactly what you should not do for an InterruptedException. An InterruptedException is basically a polite request for that thread to terminate. The thread should clean up and exit as soon as possible.

    IBM has a good article posted about this: http://www.ibm.com/developerworks/java/library/j-jtp05236.html

    Here’s what I would do:

    // Run while not interrupted.
    while(!(Thread.interrupted())
    {
        try
        {
            // Do whatever here.
        }
        catch(InterruptedException e)
        {
            // This will cause the current thread's interrupt flag to be set.
            Thread.currentThread().interrupt();
        }
    }
    
    // Perform cleanup and exit thread.
    

    The advantage to doing it this way is thus: If your thread is interrupted while in a blocking method, the interrupt bit will not be set and an InterruptedException is thrown instead. If your thread is interrupted while not in a blocking method, the interrupted bit will be set, and no exception will be thrown. So by calling interrupt() to set the flag on an exception, both cases are being normalized to the first case, which is then checked by the loop conditional.

    As an added bonus, this also lets you stop your thread by simply interrupting it, instead of inventing your own mechanism or interface to set some boolean flag to do the exact same thing.

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

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Microsoft.Jet.OLEDB doesn't have 64bit version, only 32bit. Compile your application… May 16, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer Could it be simply that you have point.y and point.x… May 16, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer In the xml side in the template, you need to… May 16, 2026 at 4:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Here are two chunks of code that accomplish (what I think is) the same
Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore ? As far as
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using SVN you can right click a folder TEST and add it to the
Using PHP, I'm trying to give each specific text its own variable. I believe
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Using an access token with offline_access (which I believe is the ultimate and most
Using the below XML, I need to figure out which person worked more hours

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.