• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About locking and RandomAccessFile

 
Daniel Breitner
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Breitner
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic