public interface LockFile extends AutoCloseable
file
.
An instance is acquired by invoking LockFileFactory.acquire(File, long)
.
All methods of this interface are thread-safe.
Important: You should never access the locked file directly. Though this
works fine on some operating systems (e.g. on GNU/Linux), it fails on others (e.g. on Windows). If you
want to read from / write to the locked file, please use createInputStream()
or
createOutputStream()
instead.
Modifier and Type | Method and Description |
---|---|
void |
close()
Equivalent to
release() . |
IInputStream |
createInputStream()
Creates an
InputStream reading from the locked file. |
IOutputStream |
createOutputStream()
Creates an
OutputStream writing into the locked file (overwriting
all old content). |
File |
getFile()
Gets the underlying file being locked.
|
Lock |
getLock()
Gets the
Lock corresponding to the underlying file to synchronise multiple threads of the same
process. |
void |
release()
Releases the lock.
|
File getFile()
null
.void release()
Important: This method must be called exactly once for every LockFile
instance!
It is highly recommended to use a try-finally-block:
LockFile lockFile = LockFileFactory.acquire(theFile, theTimeout); try { // do something } finally { lockFile.release(); }
This method is thread-safe and thus might be invoked on a different thread than the instance
was created. However, it should be invoked exactly once (per LockFile
instance).
Please note: It is possible to use the new try-with-resources-clause introduced by Java 7. See
close()
.
LockFileFactory.acquire(File, long)
,
close()
void close()
release()
.
Implementations must make sure that invoking close()
means exactly the same as invoking
release()
. This method was added to make the usage of LockFile
possible in a
try-with-resources-clause. See AutoCloseable
for more details. Here's a code example:
try ( LockFile lockFile = LockFileFactory.acquire(theFile, theTimeout); ) { // do something while the file represented by 'lockFile' is locked. }
close
in interface AutoCloseable
release()
Lock getLock()
Lock
corresponding to the underlying file to synchronise multiple threads of the same
process.
A LockFile
(usually implemented using FileLock
) is not
guaranteed to exclude multiple threads from accessing a single file. In order to additionally provide
thread-synchronisation (as is pretty straight-forward and needed in many situations), there is a
Lock
associated to every LockFile
. It is highly recommended to synchronise additionally
on this Lock
. Note, that createInputStream()
and createOutputStream()
are
expected to implicitly do this.
Lock
corresponding to the underlying file. Never null
.IInputStream createInputStream() throws IOException
InputStream
reading from the locked file.
Important: You must close the InputStream
! Locks held
are released only when doing so.
InputStream
reading from the locked file. Never
null
.IOException
- if creating the InputStream
fails.IOutputStream createOutputStream() throws IOException
OutputStream
writing into the locked file (overwriting
all old content).
Important: You must close the OutputStream
! Locks
held are released only when doing so.
OutputStream
writing into the locked file. Never
null
.IOException
- if creating the OutputStream
fails.Copyright © 2013–2019. All rights reserved.