wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RandomAccessFile.length() is not thread save! 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 "RandomAccessFile.length() is not thread save!" Watch "RandomAccessFile.length() is not thread save!" New topic
Author

RandomAccessFile.length() is not thread save!

Tomas Klubal
Greenhorn

Joined: Aug 16, 2004
Posts: 20
Hi,
I think that it might be interesting for other ranchers to know that RandomAccessFile.length() is not thread save. File pointer is moved during this operation before it is set back to the original position. I have experienced EOFExceptions when retrieving all records even I had seek/read and seek/write operations synchronized.

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823133 for details.

Regards,
Jonathan Wolter
Greenhorn

Joined: Nov 26, 2007
Posts: 12
In many exam problems, we may assume only one "Server application" -- and if you choose, one thread - is accessing the database file. Do you have an implementation that suggests this thread safety issue is a problem? I must be missing something...

Jonathan
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
Hi,
One solution that I can think of is opening a random access file in each method separately. That would solve closing the file and the unsynchronized length().


SCBCD - SCWCD - SCJD - SCJP - OCA
Tomas Klubal
Greenhorn

Joined: Aug 16, 2004
Posts: 20
Originally posted by Musab Al-Rawi:
Hi,
One solution that I can think of is opening a random access file in each method separately. That would solve closing the file and the unsynchronized length().

My solution is to synchronize on the random access file when calling length and assign the length to the local variable before iterating through all records during search.
Tomas Klubal
Greenhorn

Joined: Aug 16, 2004
Posts: 20
Originally posted by Jonathan Wolter:
In many exam problems, we may assume only one "Server application" -- and if you choose, one thread - is accessing the database file. Do you have an implementation that suggests this thread safety issue is a problem? I must be missing something...

Jonathan

I have implementation that uses multiple threads accessing the same instance of the RandomAccessFile.

For instance while thread A is reading all records to perform search on them, thread B is trying to read (or update or delete) particular record.

I have experienced following:
  • Thread-B sets the file pointer to ie. 192 and is sliced out
  • Thread-A thread calls length() and inside this operation moves the file pointer to the end of the file and is sliced out before it can return file pointer to the original position and finish the length() operation.
  • Thread-B tries to read from the incorect position set by Thread-A and throws EOFException.
  • Thread-A finishes the length() method by setting the file pointer to the original position

  • ciaran_cahill
    Greenhorn

    Joined: Oct 01, 2007
    Posts: 5
    Wouldn't it be best to have a lock for the RandomAccessFile object and enforce a design rule that programmers must acquire this lock before using the object in any way?
    Tomas Klubal
    Greenhorn

    Joined: Aug 16, 2004
    Posts: 20
    Originally posted by CP Cahill:
    Wouldn't it be best to have a lock for the RandomAccessFile object and enforce a design rule that programmers must acquire this lock before using the object in any way?


    Yes,
    that's my solution. Instance of the RandomAccessFile is used as a lock object and every access to it is synchronized.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: RandomAccessFile.length() is not thread save!
     
    Similar Threads
    IndexOutOfBoundsException at javax.faces.component.UICompo
    SCJP Brainteaser (9)
    what is wrong with these two codes?
    RandomAccessFile.length() is NOT thread safe
    Available space for file on server