Solved

multi-threading, concurrent writes / reads

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

728 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