The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird Locking Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "URLyBird Locking" Watch "URLyBird Locking" New topic
Author

URLyBird Locking

Rod Sinclair
Greenhorn

Joined: May 11, 2010
Posts: 22
Hi

I am implementing the locking in URLyBird assignment. According to the spec you have to lock on an individual record before updating the database. So if different threads want to update different records they can do this simultaneously.

I dont have any problems implementing the code but was just wondering if this actually works ok when using a RandomAccessFile for the database. i.e. can two different threads write a RandomAccessFile (writing to different parts of the file) conncurrently without causing corruption.

Regards

Rod
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 3740

Hi Rod,

Yes we can on 1 condition: you have to make sure writing to the database file happens in a thread-safe manner. More concrete: positioning the file pointer and writing the actual bytes should happen as an atomic operation (so no other thread can move the file pointer to another position)

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
 
subject: URLyBird Locking
 
developer file tools