I work with threads and I would like that if a thread 1 does any work in a folder, that another thread 2 waits until thread 1 is done with its work. Is there any way to lock access to folders, the same way as FileLock
from java.nio
?

- 409
- 1
- 5
- 12
-
2You might be going straight for a mistake depending on what you actually want to do. Is the point working in parallel? Because if that's the case your entire solution must be based around that and that does not require locks at all. – Emanuel Couto May 24 '20 at 14:03
-
1You might also want to check this thread: https://stackoverflow.com/questions/1040828/how-do-i-use-the-linux-flock-command-to-prevent-another-root-process-from-deleti FileLock is operating system dependent. In Linux it will not give you much guarantees. I'm not sure about Windows but I wouldn't rely on it if it's not cross platform. This is just to show you that perhaps you're thinking that file locking will lock your file for every call in your operating system and that's not actually the case. You should think of it as advisory. – Emanuel Couto May 24 '20 at 14:08
1 Answers
The FileLock
API mirrors whatever your OS feature for file locking does. The point of it is to lock access to the file for all processes running on your system, not just your VM. The way you talk about 'that another thread 2 waits until thread 1 is done with its work' sounds like you want locks within your own VM (i.e. that the 'threads' you speak of, are your code). Because FileLock mirrors the OS, it's finicky - it works differently depending on which platform your run on, which makes testing a nightmare.
You shouldn't use it if your aim is solely to manage your own application's threads.
First the bad news: What you want does not exist - there is no directory-level locking that acts like a file lock on all files as well as making/deleting anything within a directory at the OS level, and therefore java doesn't have it either.
Now the good news: But you didn't want it anyway - and what you do want does exist: You just want.. locks, plain and simple. You can use the various canonicalizing paths methods in the java API, such as Path.toRealPath()
to end up with 'real' paths, and then use a while loop to repeatedly call Path.getParent()
to build a bunch of objects representing the parent paths involved for any file.
Java has lock functionality, such as ReentrantReadWriteLock
(that package contains simpler lock features too, and java.util.concurrent in general contains lock-like constructs, such as latches).
Now all that's left is tying the two concepts together. You're going to have to decide how to map a (parent) directory to a lock, and which lock(s) need to be held in order to operate on a file. You can use a ConcurrentHashMap
to map Path
objects to Lock
or ReentrantLock
objects, perhaps.
You'd have to add code to sort out the locking before doing any file access from java, of course. If you want a failsafe to detect that you've sorted it all out, you can install a SecurityManager
if you must; this acts like a hook that is invoked prior to any disk access.

- 85,357
- 5
- 51
- 72