Search code examples
javamultithreadingfile-iofile-locking

Locking a file for an individual Thread within a VM


I have multiple threads which are serializing my 'Data' objects to files. The filename is based on 2 fields from the Object

  class Data {
    org.joda.DateTime time;
    String title;

    public String getFilename() {
      return time.toString() + '_' + title + ".xml";
    }

It is possible that 2 Data objects will have the same 'time' and 'title', and so the same filename.

This is acceptable, and I'm happy for either to be saved. (They're probably the same Data object anyway if those are the same)

My problem is that two (or more) threads are writing to a file AT THE SAME TIME, causing malformed XML.

I had a look at java.nio.channels.FileLock, but it's for VM-Wide locking, and specifically NOT suitable for intra-Thread locking.

I could synchronize on DataIO.class (but that will cause a HUGE overhead, since I really only want to synchronize on the individual File).

Synchronizing on the File object will be useless, as multiple File objects can represent the same System-File.

Code Follows:

class DataIO {
  public void writeArticleToFile(Article article, String filename, boolean overwrite) throws IOException {
    File file = new File(filename);
    writeArticleToFile(article, file, overwrite);
  }

  public void writeDataToFile(Data data, File file, boolean overwrite) throws IOException {
    if (file.exists()) {
      if (overwrite) {
        if (!file.delete()) {
          throw new IOException("Failed to delete the file, for overwriting: " + file);
        }
      } else {
        throw new IOException("File " + file + " already exists, and overwrite flag is set to false.");
      }
    }

    File parentFile = file.getParentFile();
    if (parentFile != null) {
      file.getParentFile().mkdirs();
    }

    file.createNewFile();

    if (!file.canWrite()) {
      throw new IOException("You do not have permission to write to the file: " + file);
    }

    FileOutputStream fos = new FileOutputStream(file, false);
    try {
      writeDataToStream(data, fos);
      logger.debug("Successfully wrote Article to file: " + file.getAbsolutePath());
    } finally {
      fos.close();
    }
  }
}

Solution

  • You could intern() the string that is the filename. Then synchronise on the interned string.

    class DataIO {
      public void writeArticleToFile(Article article, String filename, boolean overwrite) throws IOException {
        synchronized(filename.intern()) {
           File file = new File(filename);
           writeArticleToFile(article, file, overwrite);
        }
      }