Code example for FileLock

Methods: isValid, release

0
        try(AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path,WRITE)) {
 
            fileChannel.lock("gaopengxiang", new CompletionHandler<FileLock, String>() {
                @Override 
                public void completed(FileLock result, String attachment) {
                    if (result.isValid()) {
                        System.out.println("try to release lock...");
 
                        try { 
                            result.release();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } 
                    } 
                } 
 
                @Override 
                public void failed(Throwable exc, String attachment) {
                    System.out.println(attachment);
                    System.out.println("error is:"+exc.getMessage());
                }