This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to solve the Reader and Writer problem using wait() and notify()? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "How to solve the Reader and Writer problem using wait() and notify()?" Watch "How to solve the Reader and Writer problem using wait() and notify()?" New topic
Author

How to solve the Reader and Writer problem using wait() and notify()?

Lion Z. Li
Greenhorn

Joined: Oct 31, 2003
Posts: 19
Hi everyone,
Suppose there is shared HashMap and several reader and writer threads, how can we solve this concurrency problem using wait() and nofity() instead of synchronized method, if possible?


SCJP1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
You can't use wait() or notify() without also synchronizing - you'd get an IllegalMonitorStateException. Plus, it's not at all clear what you're trying to do here. What do the different threads need to wait for? What' wrong with using synchronization to protect a shared resource?


"I'm not back." - Bill Harding, Twister
Lion Z. Li
Greenhorn

Joined: Oct 31, 2003
Posts: 19
Jim Yingst,
Yes, you are right. There is nothing wrong with protecting shared data (that may bring about race condition) by using synchronization. We can utilize synchronizing in this reader/writer example.
in above codes, both reads() and writes() are synchronized on the same HashMap object and thus shared data can be safely protected. However, in pratice when it comes to the case of reads()/reads() invocations in differrent threads we dont have to wait on synchronization. i.e. above may result in performance hit when there are many Readers involved. My question is IS there any solution better than this synchronizing-all one?(and i guess in this better solution wait()/notify() might show up).
THANKS FOR YOUR HELP!
[ November 14, 2003: Message edited by: LION Z. LI ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
There may be faster solutions, though I would usually advocate just using normal synchronizatin first, and see how good your performance is. Alternate solutions will probably be more complex, and may not be worth the extra effort.
Are reads more common than writes? Then you could use two maps - one, an unsynchronized map that you never make changes to, which you use for most of your accesses. Then you have another copy that you do make changes to, with sync. This is the master copy, with all data - but since it requires sync, you try no to use it too often. Periodically you can refresh the unsynchronized read-only copy by making a clone() of the master copy, then replacing the read-only copy with the new clone. This means that when lookups are performed on the read-only copy, then may not get the most recent information, unless a refrresh was performed recently. But this may be perfectly acceptable for some applications.
Note - some people may suggest that you don't need to make copies here, that you can syncronize for writes but not for reads using a single HashMap. This is usually a bad idea. In particular, if a put() ever causes the HashMap to resize while a read() is in effect, that read() may get completely erroneous data, or throw a ConcurrentModificationException or maybe even NullPointerException or IndexOutOfBoundsException. (It's hard to guarantee jsut what will happen.) It's one thing to retrieve data that's slightly old; it's another to throw an exception. (Even for data that's really in the HashMap; it was just being moved.) So be very careful if you try something like this.
I can't really think of any solutions involving wait/notify here. There might be one, but it's not coming to mind. Wait/notify is often a good efficient protocol, but I can't imagine how it would apply here.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Doug Lea's util.concurrent package may have what you're looking for: a Map implementation synchronized on a read-write lock.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to solve the Reader and Writer problem using wait() and notify()?
 
Similar Threads
what is the difference between stream classes and reader,writer classes
problem with my threading
downloading images?
Who's using threads?
Wait some time before execute next line in my code.