wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes About locking and RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "About locking and RandomAccessFile" Watch "About locking and RandomAccessFile" New topic
Author

About locking and RandomAccessFile

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Hello everybody ... I am just starting getting involved with my SCJD exam and I already have implemented my data layer: A simple class reading and writing to the dataFile using a RandomAccessFile (RAF).

Now my question ...

why do you all use one RAF that you share between Threads ?

Isn´t it much easier to use individual RAF instances for each request?

best regards

daniel


Visit me at http://liferay-blogging.blogspot.com
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, Daniel!

Champion, I don't think this could be possible. The file is unique, so someone will have to be Singleton, so the file doesn't get messed up. My Data class is Singleton, but some people choose to create the Data class as a Facade, and divide the responsibilities in a class that only reads the file and another one that manages the locking mechanism. This class that deals with the file has only one RandomAccessFile object, and then there's the possibility to either synchronize on this object, or to synchronize on the object that deals with the file in the Facade class.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Ok sorry, I still don´t get it.

When I´m using a ReadWriteLock approach that makes sure that only read requests can be run simultanously and not write requests - what can be messed up if I instantiate a RandomAccessFile everytime I need one ?

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
To be more precise:

How about doing it this way:

There is one SIngleton: The Lock-Manager. It handles all locks from all clients by the ReadWriteLock Pattern.

Is this sufficient for the locking part of scjd or did I forget sth ?



Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, Daniel!

Is this sufficient for the locking part of scjd or did I forget sth ?


Well, I think it is. Honestly, since I used the classical synchronization, I'd have to test to see if it works properly, but I think it can do the job. But the important thing is to make sure that, when a Thread tries to lock a record that is already locked by another Thread, then this Thread consumes no CPU cycles when waiting for it to be released.
sohrab khan
Greenhorn

Joined: Sep 10, 2007
Posts: 13
Daniel Breitner wrote:Ok sorry, I still don´t get it.

When I´m using a ReadWriteLock approach that makes sure that only read requests can be run simultanously and not write requests - what can be messed up if I instantiate a RandomAccessFile everytime I need one ?



You don't need to instantiate a RAF everytime you need to read or write a record. It is not a good design, you can instantiate a single RAF object, store it in memory and use it whenever you need it.


Sohrab Khan SCJP, SCJD, OCPDBA, MCSE
3tons Web Directory | EggDir Directory
 
Consider Paul's rocket mass heater.
 
subject: About locking and RandomAccessFile