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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:29:36+00:00 2026-06-10T11:29:36+00:00

A seemingly straightforward problem: I have a java.util.concurrent.Semaphore , and I want to acquire

  • 0

A seemingly straightforward problem: I have a java.util.concurrent.Semaphore, and I want to acquire a permit using acquire().

The acquire() method is specified to throw InterruptedException if the thread is interrupted:

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting for a permit,

then InterruptedException is thrown and the current thread’s interrupted status is cleared.

However, the usual pattern with methods that may throw InterruptedException is to call them in a loop, since threads can be subject to spurious wakeups that look the same as being interrupted. For example, the documentation for Object.wait(long) says:

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops.

So the question is, is Semaphore.acquire() subject to the same kind of spurious wakeup? The logical answer would be “no”, but I can’t find any evidence for that, and in fact the evidence seems to point in the other direction.

Looking at the source for Semaphore, it appears that it delegates the actual acquire to an AbstractQueuedSynchronizer, which according to its source delegates to LockSupport.park().

The documentation for LockSupport.park() explicitly mentions spurious wakeup, but the implementation of AbstractQueuedSynchronizer.doAcquireInterruptably() appears to just check Thread.interrupted() and then throw InterruptedException.

So, unless I’m missing something (which is very possible), it appears that Semaphore.acquire() can throw InterruptedException spuriously?

Is that correct? More importantly, is there anything I can do about that? I could use Semaphore.acquireUninterruptably(), but I don’t want an uninterruptable wait, just one that doesn’t get interrupted spuriously. Is there any alternative?

  • 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-10T11:29:38+00:00Added an answer on June 10, 2026 at 11:29 am

    It is “spurious wakeup” not “spurious interrupt”: “A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup.” There is no InterruptedException thrown during a spurious wakeup. As you say in the comments: The thread wakes up but the interrupted flag is not set.

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

Sidebar

Related Questions

I have a seemingly simple problem whereby I wish to reconcile two lists so
I have a seemingly basic problem with C# arrays. I am a beginner, but
I have seemingly easy problem and am having trouble designing the right solution. I
A seemingly trivial problem. I have a very basic page in Visual Studio 2010.
I'm having a seemingly silly problem with my gcc compiler. I have installed MinGW
I've got a seemingly small problem using LaTeX, which i can't find the answer
My program is seemingly having a deadlock problem. Basically I have a class that
I have a seemingly simple problem though i am unable to get my head
Seemingly trivial problem in assembly: I want to copy the whole XMM0 register to
Hi I have seemingly simple problem which is causing my head to hurt. I

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.