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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:51:18+00:00 2026-06-13T16:51:18+00:00

It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers. Nevertheless,

  • 0

It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers.

Nevertheless, readers should wait until some data is present in the buffer.

So, what to lock?

I created concurrency objects like follows:

private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
protected final Lock readLock = rwl.readLock();
protected final Lock writeLock = rwl.writeLock();
protected final Condition hasData = writeLock.newCondition();

now in write method I do:

writeLock.lock();

// writing first portion and updating variables

hasData.signalAll();

// if required then writing second portion and updating variables

hasData.signalAll();

But how to write a reader? Should it acquire only readLock? But how it can wait for a signal then? If it aquires also a writeLock then where is the supremacy fo read/write locking?

How to ensure required variables will not change during reading if they are protected only by writeLock?

QUEUES DON’T MATCH THE TASK

This is the question about ReentrantReadWriteLock.

  • 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-13T16:51:19+00:00Added an answer on June 13, 2026 at 4:51 pm

    The ReentrantReadWriteLock is indeed a bit confusing because the readLock doesn’t have a condition.
    You have to upgrade to a writeLock in your reader only to wait for the condition.

    In the writer.

    writeLock.lock(); //locks all readers and writers
    // do write data
    hasData.signalAll();
    writeLock.unlock();
    

    In reader you do:

    readLock.lock(); //blocks writers only
    try{
     if(!checkData()) //check if there's data, don't modify shared variables
     {
      readLock.unlock();
      writeLock.lock(); // need to lock the writeLock to allow to use the condition.
                        // only one reader will get the lock, other readers will wait here      
      try{
       while(!checkData()) // check if there' still no data
       {
         hasData.await(); //will unlock and re-lock after writer has signalled and unlocked.
       }
       readLock.lock();    // continue blocking writer
      }
      finally
      {
        writeLock.unlock(); //let other readers in
      }
     }
     //there should be data now
     readData(); // don't modify variables shared by readers.
    }
    finally
    {
      readlock.unlock(); //let writers in
    }
    

    For completeness, each unlock() should be in a finally block, of course.

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

Sidebar

Related Questions

It is said that in order to prevent from SQL injection one should filter
No one said that OrderedDictionary is having two copies of elements, one in a
Doctrine2 docs said that soft-delete behavior should be better implemented as a State pattern
On various places they said that you should use @ManagedProperty to get a request
Its said that property should not be Set only (Code analysis rule CA1044 )and
It is said that when input parameter is null, compareTo() should throw a NullPointerException.
It is often said that, you should not rebase commits that you have already
A programmer I respect said that in C code, #if and #ifdef should be
Many people said that one can achieve higher performance by creating read-only and write-only
Could it be said that when you reach the point of injecting one DAO

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.