Solved

multi-threading, concurrent writes / reads

Posted on 2013-05-27
3
260 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 35

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 35

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Java: How do I open the default windows program for a given file type 3 53
Java Restore security prompts not working 10 140
java example issue 5 45
collection output issue 9 65
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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