Solved

multi-threading, concurrent writes / reads

Posted on 2013-05-27
3
254 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
  • 2
3 Comments
 
LVL 35

Accepted Solution

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

Author Comment

by:Squadless
Comment Utility
actually a very good solution in my opinion. thanks.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now