Class SynchronizedDirectory

java.lang.Object
com.nt.udc.ei.transport.SynchronizedDirectory

public class SynchronizedDirectory extends Object
This class is used to let threads synchronize on a common directory into which and from which files are placed and processed. This was needed in order for one or more threads to handle populating the directory (as producers) and one or more other threads to process the files (as consumers). This class not only provides a lock and unlock capability, but it also provides methods for letting the producer notify the consumer when files are available.
  • Field Details

    • lockOwner

      protected Thread lockOwner
    • busyCount

      protected int busyCount
  • Constructor Details

    • SynchronizedDirectory

      public SynchronizedDirectory(File dir)
      Instantiate with a known directory. This is the directory into which the producer will place files and from which the consumer will process files.
  • Method Details

    • lock

      public void lock()
      Used to "grab" the lock on the directory of interest. This method will wait until the lock is available.
    • unlock

      public void unlock()
      Release the lock on the directory. This method keeps track of reentrant locks via the busyCount variable.
    • getDirectory

      public File getDirectory()
      Returns the directory of interest.
    • waitForMoreFiles

      public String[] waitForMoreFiles(FilenameFilter f)
      Used by a thread that consumes files from the directory of interest. If no files are there, this method will cause the thread to wait until a producer places files in the directory.
      Parameters:
      f - A required FilenameFilter for identifying only files of interest.
      Returns:
      an array with the names of all the files found
    • waitForMoreFiles

      public List<File> waitForMoreFiles(DirectoryStream.Filter<Path> filter) throws Exception
      Used by a thread that consumes files from the directory of interest. If no files are there, this method will cause the thread to wait until a producer places files in the directory.
      Parameters:
      filter - A required DirectoryStream.Filter for identifying only files of interest.
      Returns:
      a List with the names of all the files found
      Throws:
      Exception
    • notifyMoreFiles

      public void notifyMoreFiles()
      Used by a producer to notify any consumers that files have been placed in the directory of interest. Onc common approach to using this method is as follows:


      File myDir = new File("some_directory"); SynchronizedDirectory sd = new SynchronizedDirectory(myDir); . . . // time to place files in the directory: sd.lock(); // Now that the directory is locked: produceFiles(); // Now unlock and notify: sd.unlock(); sd.notifyMoreFiles();