I'm using multiple JVMs, but I need that each JVM to use a specific folder. What I'm trying to do is iterate through folders till it finds a file that is not locked and then lock it to use that specific folder.
Here I'm filtering the folders I want to use:
// Filter 'fran' folders
String dir = System.getProperty("user.dir");
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
String lowercaseName = name.toLowerCase();
if (lowercaseName.startsWith("fran")) {
return true;
} else {
return false;
}
}
};
File[] dirs = new File(dir).listFiles(filter);
Then I'm trying to go through the folders and check if it is locked or not with f.canWrite()
. However it always appears to use only one folder and ignore the others.
// Find available folder
boolean lock = true;
String lock_folder = "";
FileChannel fileChannel = null;
FileLock lockfile = null;
File f = null;
while (lock) {
for (File folder : dirs) {
f = new File(folder + "\\lock.txt");
Boolean isnotlocked = f.canWrite();
if (isnotlocked) {
fileChannel = new RandomAccessFile(f, "rw").getChannel();
lockfile = fileChannel.lock();
lock = false;
lock_folder = folder.getAbsolutePath();
break;
}
}
}
I+ve previously tried to accomplish what I needed without FileLock, creating a file in the specific folder and then deleting after completed. If the folder did not have that file it would create and lock that JVM. However I think the JVMs were getting mixed cause the results were bad.
Hope u can understand what my problem is, would really appreciate some help.
Here are some ideas:
Assuming a process class - CustomProcess.java
. This runs in a separate thread. The class has a constructor which takes a file folder as an argument. Lets assume the filePath1
is the folder's path and is accepted from a FileChooser
.
(a) How the application works:
Put the selected folder filePath1
in a collection like List<File>
or List<Path>
- lets call it processFilesList
; this is shared (perhaps a static
member) by all the processes (this needs to be a concurrent collection from java.util.concurrent
package). This list tracks the folders which are already being processed. Before the process starts check if the filePath1
is already in the processFilesList
.
(b) Create and start the process:
CustomProcess p1 = new CustomProcess(filePath1);
p1.startProcess(); // here the application does whatever with the files in the folder.
Option 2 :
Put all the folder file paths that need to be processed in a Queue
collection. Process each folder (and its files as needed) one at a time or by multiple processes. The queue can be a first-in-first-out (FIFO) or a last-in-first-out (LIFO). One can consider these concurrent queue implementations based on the requirement: ConcurrentLinkedQueue
, LinkedBlockingQueue
, ConcurrentLinkedDeque
or LinkedBlockingDeque
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.