Package com.nt.udc.ei.transport
Class SynchronizedDirectory
java.lang.Object
com.nt.udc.ei.transport.SynchronizedDirectory
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the directory of interest.void
lock()
Used to "grab" the lock on the directory of interest.void
Used by a producer to notify any consumers that files have been placed in the directory of interest.void
unlock()
Release the lock on the directory.String[]
Used by a thread that consumes files from the directory of interest.waitForMoreFiles
(DirectoryStream.Filter<Path> filter) Used by a thread that consumes files from the directory of interest.
-
Field Details
-
lockOwner
-
busyCount
protected int busyCount
-
-
Constructor Details
-
SynchronizedDirectory
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
Returns the directory of interest. -
waitForMoreFiles
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
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.Filterfor 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();
-