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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:56:30+00:00 2026-06-04T12:56:30+00:00

I need a ReadWriteLock that is NOT reentrant, because the lock may be released

  • 0

I need a ReadWriteLock that is NOT reentrant, because the lock may be released by a different thread than the one that acquired it. (I realized this when I started to get IllegalMonitorStateException intermittently.)

I’m not sure if non-reentrant is the right term. A ReentrantLock allows the thread that currently holds to lock to acquire it again. I do NOT want this behaviour, therefore I’m calling it “non-reentrant”.

The context is that I have a socket server using a thread pool. There is NOT a thread per connection. Requests may get handled by different threads. A client connection may need to lock in one request and unlock in another request. Since the requests may be handled by different threads, I need to be able to lock and unlock in different threads.

Assume for the sake of this question that I need to stay with this configuration and that I do really need to lock and unlock in different requests and therefore possibly different threads.

It’s a ReadWriteLock because I need to allow multiple “readers” OR an exclusive “writer”.

It looks like this could be written using AbstractQueuedSynchronizer but I’m afraid if I write it myself I’ll make some subtle mistake. I can find various examples of using AbstractQueuedSynchronizer but not a ReadWriteLock.

I could take the OpenJDK ReentrantReadWriteLock source and try to remove the reentrant part but again I’m afraid I wouldn’t get it quite right.

I’ve looked in Guava and Apache Commons but didn’t find anything suitable. Apache Commons has RWLockManager which might do what I need but I’m not sure and it seems more complex than I need.

  • 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-04T12:56:32+00:00Added an answer on June 4, 2026 at 12:56 pm

    A Semaphore allows different threads to perform the acquire and release of permits. An exclusive write is equivalent to having all of the permits, as the thread waits until all have been released and no additional permits can be acquired by other threads.

    final int PERMITS = Integer.MAX_VALUE;
    Semaphore semaphore = new Semaphore(PERMITS);
    
    // read
    semaphore.acquire(1);
    try { ... }
    finally {
      semaphore.release(1);
    }
    
    // write
    semaphore.acquire(PERMITS);
    try { ... }
    finally {
      semaphore.release(PERMITS);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need a little help with this one. I have a form that I am
Need to know how to get YQL to return only data that's newer than
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some advice to use iframe for widget or not. Will some addons/plugin block
Need your help with my PHP/MYSQL array. I have a php script that selects
Need help with the codeigniter, I think file_exists is for server path, not for
Need help with an error message that I just can't figure out. I am
Need help for that. I have a tabbar with 2 views, A and B
Need a way for one service on a well-known Endpoint to return strings which
Need some help please with this error : TypeError : 'unicode' object does not

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.