permaculture playing cards*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Concurrency problem in URLyBird Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Concurrency problem in URLyBird" Watch "Concurrency problem in URLyBird" New topic
Author

Concurrency problem in URLyBird

Xin Gang Sun
Greenhorn

Joined: Sep 03, 2009
Posts: 17
Hi,
Data.java puzzled me. My Data class extends java.io.RandomAccessFile and is not singleton pattern. All methods marked synchronized on static variable. Every record has a Read-Lock and Update-Lock. If a record is locked on its Update-Lock, no one can read and update it, and if it is locked on its Read-Lock, other threads can read but not update it. I use the lockCookie as the Update-Lock. In order to implement, I have a complex synchronized mechanism by using two java.util.concurrent.ConcurrentHashMap to store the locks for every record.
Is it necessary to be so complex?
Do I comprehend the instructions.html and the interface (DBAccess) wrongly?


Xin Gang Sun
boolean scjp_6 = true;
boolean scjd_URLyBird = willBeTrue();
//I hope!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

Hi Xin,

Your Data class extends java.io.RandomAccessFile? Or just use it? About your approach, it looks like you are using the read-writelock design pattern with those read-lock/update-lock flags. Honestly, your current approach is a bit complex IMO. Do you really need 2 ConcurrentHashMaps?

When I first did my locking mechanism, I also tried using the read/write lock design pattern and sort of come across what you are expecting - some where there are records waiting meaning deadlock. The contention is really comes from when to set those flags on/off. However, this design pattern is indeed easy to understand yet quite hard to get it perfect.

Then I created my own class for locking using some singleton collection (ie only one record can be present in the collection) to control what record is currently locked. Now thinking back on this, I could have simplified this approach a bit more from what I have done.

Anyway hope I shed some light.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Xin Gang Sun
Greenhorn

Joined: Sep 03, 2009
Posts: 17
Thank you, K. Tsang.
My Data extends java.io.RandomAccessFile and implements DBAccess. In my program, every thread has a Data instance to access data file.
You are right, it looks like the read/write lock design pattern. ConcurrentHashMap is necessary because it is atomic, it is important in my methods in order to ensure the locks thread-safe.
Now I have finished it, though it is hard to debug.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Xin,

Like K. Tsang already pointed out: I don't think extending from RAF is a good approach, actually it is the first time I hear someone extending from RAF.

This would be a more appropriate approach:


Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Xin Gang Sun
Greenhorn

Joined: Sep 03, 2009
Posts: 17
Thanks, K. Tsang and Roel.
You are right. It is not logical.
I modified my design.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

Hi Xin, if you are using RAF then why you need a separate class that extends RAF? Roel and I were saying more like:


About your RandomAccessDatabaseFile ... more of an interface from the code you provided.
Xin Gang Sun
Greenhorn

Joined: Sep 03, 2009
Posts: 17
Hi, K. Tsang.
Yes, but I think two designs are the same. My RADF class extends RAF and add readRecord() and writeRecord() method. In Data methods, readRecord()(a method in my RADF) instead of readXxx() can be more clear in my code. Actually, they are the same.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

But would you want your Data class calling the java.io.RandomAccessFile#write(byte[]) method of your RADF class directly? It is possible if you extend RAF.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Xin Gang Sun
Greenhorn

Joined: Sep 03, 2009
Posts: 17
Yes, it can call all of the methods. I think that making a RADF class is more Object-Oriented. But it is unnecessary actually.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrency problem in URLyBird
 
Similar Threads
locking using notify/wait
Some Locking Advice
consumes no CPU cycles?
Locking problem
B&S 2.2.2 - Network Layer Query