title: Is a read lock on a ReentrantReadWriteLock sufficient for concurrent reading of a RandomAccessFile
body: <p>I'm writing something to handle concurrent read/write requests to a database file.</p>
  
  <p><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html" rel="nofollow noreferrer">ReentrantReadWriteLock</a> looks like a good match.  If all threads access a shared <a href="http://java.sun.com/javase/6/docs/api/java/io/RandomAccessFile.html" rel="nofollow noreferrer">RandomAccessFile</a> object, do I need to worry about the file pointer with concurrent readers?  Consider this example:</p>
  
  <pre><code>import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.RandomAccessFile;
  import java.util.concurrent.locks.ReentrantReadWriteLock;
  
  public class Database {
  
      private static final int RECORD_SIZE = 50;
      private static Database instance = null;
  
      private ReentrantReadWriteLock lock;
      private RandomAccessFile database;
  
      private Database() {
          lock = new ReentrantReadWriteLock();
  
          try {
              database = new RandomAccessFile("foo.db", "rwd");
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
      };
  
      public static synchronized Database getInstance() {
          if(instance == null) {
              instance = new Database();
          }
          return instance;
      }
  
      public byte[] getRecord(int n) {
          byte[] data = new byte[RECORD_SIZE];
          try {
              // Begin critical section
              lock.readLock().lock();
              database.seek(RECORD_SIZE*n);
              database.readFully(data);
              lock.readLock().unlock();
              // End critical section
          } catch (IOException e) {
              e.printStackTrace();
          }
          return data;
      }
  
  }
  </code></pre>
  
  <p>In the getRecord() method, is the following interleaving possible with multiple concurrent readers?</p>
  
  <blockquote>
    <p>Thread 1 -> getRecord(0)<br>
    Thread 2 -> getRecord(1)<br>
    Thread 1 -> acquires shared lock<br>
    Thread 2 -> acquires shared lock<br>
    Thread 1 -> seeks to record 0<br>
    Thread 2 -> seeks to record 1<br>
    Thread 1 -> reads record at file pointer (1)<br>
    Thread 2 -> reads record at file pointer (1)  </p>
  </blockquote>
  
  <p>If there are indeed potential concurrency issues using ReentrantReadWriteLock and RandomAccessFile, what would an alternative be?</p>