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?
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.
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 ?
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.
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.