Class FileLock

  • All Implemented Interfaces:
    AutoCloseable

    public final class FileLock
    extends Object
    implements AutoCloseable
    Helper class to lock a file. It is recommended practice to lock a file with a try with resources block, such as:
     
     class X {
       public void m() {
         try(FileLock lock = FileLock.of(lockFile)){
             if(lock.tryLock(1, TimeUnit.MINUTES){
                 // locked
             } else {
                 // not locked
             }
         }
     }}
     
    An instance of this class must not be shared across different threads.
    Since:
    4.0.0
    Author:
    Dmytro Nosan