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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:15:29+00:00 2026-05-23T17:15:29+00:00

I have been looking at the source of ArrayBlockingQueue . I am trying to

  • 0

I have been looking at the source of ArrayBlockingQueue. I am trying to understand why conditions are signalled when catching InterruptedException‘s. Here is an example, notice the notFull.signal() call before the InterruptedException is rethrown. Why is this necessary? If there are 2 threads simultaneously invoking offer and one is interrupted, wouldn’t the other thread then enter the critical section guarded by the lock and then see a count < items.length?

public boolean offer(E e, long timeout, TimeUnit unit)
    throws InterruptedException {

    if (e == null) throw new NullPointerException();
long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        for (;;) {
            if (count != items.length) {
                insert(e);
                return true;
            }
            if (nanos <= 0)
                return false;
            try {
                nanos = notFull.awaitNanos(nanos);
            } catch (InterruptedException ie) {
                notFull.signal(); // propagate to non-interrupted thread
                throw ie;
            }
        }
    } finally {
        lock.unlock();
    }
}
  • 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-23T17:15:30+00:00Added an answer on May 23, 2026 at 5:15 pm

    It is entirely possible that a thread waiting on await will be signalled and interrupted, and that the interruption will take precedence. The following code demonstrates this:

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class SignalTest {
        public static void main(String... args) throws InterruptedException {
            for ( int i = 0; i < 2000; i++ ) {
                tryOnce();
            }
        }
    
        private static void tryOnce() throws InterruptedException {
            final Lock lock = new ReentrantLock();
            final Condition condition = lock.newCondition();
    
            Thread t = new Thread(new Runnable() {
                public void run() {
    
                    try {
                        lock.lockInterruptibly();
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
    
            t.start();
    
            Thread.sleep(1);
            lock.lock();
            condition.signal();
            t.interrupt();
            lock.unlock();
        }
    }
    

    For me, 2-10 of the 2000 attempts result in an InterruptedException, even though I’m signalling before I’m interrupting and doing both from the same thread.

    Since this is a very real possibility, if the catch block did not propogate up the signal, it could result in a permanently waiting state, even though there’s space available to add the new element. So instead the condition is signalled and if there is another waiting thread, it is woken up.

    This is safe because after being woken up, the code will always make sure that the condition on which they were woken up (that the queue was not full) is actually true (in this case, if (count != items.length)...). The code will not assume that since it was woken up, the condition must be true.

    Edit

    Also, to aid your understanding, it’s important to note that the for loop is not strictly a mutually exclusive section. If two threads call offer at the same time, the second will wait on the first to yield lock, that is true, but you must understand that calling notFull.await() releases the lock (the lock will be picked up again after await() returns). Therefore, you can have multiple threads blocked at the await() call of offer(), not simply blocked at lockInterruptably().

    Since that’s the case (there are multiple blocking await()s happening at the same time) if the interrupted thread silently ignored the signal then none of the other threads would be woken up in its place.

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

Sidebar

Related Questions

Hi I am trying to learn CSS and have been looking at the source
I have been looking at the source code of the IronPython project and the
I have been looking at the source code of raphael.js and I see a
I have been looking through some code on an open source project recently and
I have been looking over some of the android source, and the examples given
I have been looking for a stable RSS/Atom library for .NET, preferably open source
I've been looking to the OpenCV's magnificent source code to understand the score calculation.
I have been looking round for an open-source CMS framework that I can use
I have been looking around for a few hours trying to figure out how
I have been looking through the source of GLib and GObject and writing programs

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.