Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

multi-threading, concurrent writes / reads

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
Squadless
Asked:
Squadless
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Could the answer be; don't reinvent the wheel, just use a ReadWriteLock implementation?
0
 
SquadlessAuthor Commented:
actually a very good solution in my opinion. thanks.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now