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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:04:05+00:00 2026-06-03T09:04:05+00:00

I have multiple consumer threads waiting on a CountDownLatch of size 1 using await()

  • 0

I have multiple consumer threads waiting on a CountDownLatch of size 1 using await(). I have a single producer thread that calls countDown() when it successfully finishes.

This works great when there are no errors.

However, if the producer detects an error, I would like for it to be able to signal the error to the consumer threads. Ideally I could have the producer call something like abortCountDown() and have all of the consumers receive an InterruptedException or some other exception. I don’t want to call countDown(), because this requires all of my consumer threads to then do an additional manual check for success after their call to await(). I’d rather they just receive an exception, which they already know how to handle.

I know that an abort facility is not available in CountDownLatch. Is there another synchronization primitive that I can easily adapt to effectively create a CountDownLatch that supports aborting the countdown?

  • 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-03T09:04:07+00:00Added an answer on June 3, 2026 at 9:04 am

    JB Nizet had a great answer. I took his and polished it a little bit. The result is a subclass of CountDownLatch called AbortableCountDownLatch, which adds an “abort()” method to the class that will cause all threads waiting on the latch to receive an AbortException (a subclass of InterruptedException).

    Also, unlike JB’s class, the AbortableCountDownLatch will abort all blocking threads immediately on an abort, rather than waiting for the countdown to reach zero (for situations where you use a count>1).

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    
    public class AbortableCountDownLatch extends CountDownLatch {
        protected boolean aborted = false;
    
        public AbortableCountDownLatch(int count) {
            super(count);
        }
    
    
       /**
         * Unblocks all threads waiting on this latch and cause them to receive an
         * AbortedException.  If the latch has already counted all the way down,
         * this method does nothing.
         */
        public void abort() {
            if( getCount()==0 )
                return;
    
            this.aborted = true;
            while(getCount()>0)
                countDown();
        }
    
    
        @Override
        public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
            final boolean rtrn = super.await(timeout,unit);
            if (aborted)
                throw new AbortedException();
            return rtrn;
        }
    
        @Override
        public void await() throws InterruptedException {
            super.await();
            if (aborted)
                throw new AbortedException();
        }
    
    
        public static class AbortedException extends InterruptedException {
            public AbortedException() {
            }
    
            public AbortedException(String detailMessage) {
                super(detailMessage);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that has a multiple producer, single consumer model (multiple threads
Right now, I have a queue, with multiple producers and single consumer. Consumer thread
I have a Windows service that uses the producer/consumer queue model with multiple worker
I have multiple databases that I connect to using SQL*Plus in Windows (not the
I've spent today looking into lockless queues. I have a multiple producer, multiple consumer
Have a multiple producer consumer pattern producer1 - > consumer1/producer2 -> consumer2/producer 3, each
I have an application that has multiple threads processing work from a todo queue.
I need to implement a producer/consumer bounded queue, multiple consumers against a single producer.
I'm trying to implement a concurrent producer-consumer collection (multiple producers and consumers) that supports
I have multiple (8) WAR files and 1 EAR file that I want 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.