views:

37

answers:

3

Hello,

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();
    }
  }
}
+2  A: 

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);
    }
  }
Adrian Regan
Definitely the easiest solution. Not without risk though since it's best practice for the objects you lock to not be publicly accessible. (and an interned string is always available anywhere)
Kirk Woll
This is true and good practice. However you are not locking the string per se, more using the string to form the basis of a lock in your DataIO class. See modified post.
Adrian Regan
That really is a very neat solution, but as Kirk Woll says I could need that String elsewhere (particularly reading the files etc). However, if I prefix the filename with some unusual, convoluted String(prehaps the fully qualified class name), then lock on the intern() of THAT String, the chances of needing that exact String object become almost 0, and that's acceptable to me.
barryred
You can use the interned string elsewhere in your code, as long as you don't wrap it in a synchronised block. You are only using the interned string to form the lock in your DataIO class.
Adrian Regan
@Adrian, how do you mean that I would not be locking on the String, if I'm synchronized on it?
barryred
Ah, OK, so I am locked on it - i.e. if I wanted to do the same in a read class there would be a problem, but it's fine, I've gone for: final String lockString = ("xxxx1234LOCK_FOR_FILE_WRITING: " + this.getClass().getName() + filename).intern(); synchronized( lockString ){ File file = new File(filename); writeArticleToFile(article, file, overwrite); }
barryred
If you look at the code I have provided. Only one thread (per filename) can enter this block. Other threads elsewhere in your execution can read/use the value of the interned string without blocking.
Adrian Regan
+1  A: 

If I am reading this correctly you have a Data object that represents a single file.

You can consider creating a striped set based on the Data object. Possibly having a ConcurrentHashMap of

ConcurrentMap<Data,Lock> lockMap = new ConcurrentHashMap<Data,Lock>();

No when you want to write to this object you can do:

Lock lock = lockMap.get(someMyDataObject);
lock.lock();
try{
   //write object here
}finally{
   lock.unlock();
}

Keep in mind you would have to write the hashCode and equals method based on the title and DateTime

John V.
A: 

I agree that using synchronization is the technique you should use. What you need is a distinct object for each file permutation, and more importantly the same object each time. One option might be to create a class called FileLock:

public class FileLock {
    DateTime time;
    String title;

    public FileLock(DateTime time, String title) {
        this.time = time;
        this.title = title;
    }

    override equals/hashCode based on those two properties

    static Hashtable<FileLock, FileLock> unqiueLocks = new Hashtable<FileLock, FileLock>();
    static lockObject = new Object();

    public static FileLock getLock(DateTime time, String title) {
        synchronized (lockObject) {
            FileLock lock = new FileLock(time, title);
            if (unqiueLocks.ContainsKey(lock)) {
                return unqiueLocks.get(lock);
            }
            else {
                unqiueLocks.put(lock, lock);
                return lock;
            }
        }
    }
}

Then callers would use it like:

synchronized (FileLock.getLock(time, title)) {
    ...
}

Bear in mind this has a memory leak since the Hashtable keeps growing with new file/time permutations. If you need to, you could modify this technique so that callers of getLock also invoke a releaseLock method that you use to keep the Hashtable clean.

Kirk Woll