Solved

multi-threading, concurrent writes / reads

Posted on 2013-05-27
3
262 Views
Last Modified: 2013-05-27
So a very simple yet slightly confusing question that i came across on an interview.

Suppose you are in a multithreaded environment, you have some object which can be read and written to.  The idea is that many different threads should be able to access your object / collection to read it, but once a thread tries to write to it, all read operations must stop, but if someone is reading from it, others can read from it as well.

What's a good way to design this?
my solution was to have a few threads, all can read this collection lets say but once a thread tries to do a write to it, i would need to notify all other threads that they are no longer allowed to acquire the lock on this object and the threads that are currently holding the lock on the object while reading will have to stop and give up the lock for the write thread which takes priority.

whats a good way to accomplish this?

Thanks
0
Comment
Question by:Squadless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39200159
Could the answer be; don't reinvent the wheel, just use a ReadWriteLock implementation?
0
 
LVL 1

Author Comment

by:Squadless
ID: 39200168
actually a very good solution in my opinion. thanks.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39200172
Not a problem, glad to help!

I do concede that it can be a little hard sometimes to know if some functionality is already provided by Java or not, and if so, where it is.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question