java - 如何定义必须调用的方法(我自己的类的)

标签 java

我正在编写一个类来管理名为FileLocker的文件锁定。 由于java.nio.channels.FileLock,它的构建器方法锁定文件(哪个目录作为方法的参数接收)。FileLocker类还包含一个release()方法释放在其构建器方法中创建的 FileLock。问题是必须调用release()方法,否则文件的锁将永远不会被释放,并且其他进程将不被允许使用该文件(在当前JVM期间)。 所以我的问题是:是否存在一种方法来指示必须在使用FileLocker对象的所有代码中调用release()方法,以便在这些代码编译期间如果不使用release()方法会抛出错误? (这里我发布FileLocker类代码,只是为了展示它是如何工作的):

package essentialServer_Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import essentialServer_Exception.EssentialServerException;

public class FileLocker implements ServerConfiguration{

String fileDirectory;
int filelockingAttempts;
int maxFilelockingAttempts;
FileChannel fileChannel;
RandomAccessFile randomAccessFile;
FileLock lock;
file f;

public FileLocker (String fileDirectory) throws EssentialServerException {

    this.fileDirectory = fileDirectory;
    filelockingAttempts = 0;
    maxFilelockingAttempts = maxDatabaseFilesLockingAttempts();
    f = new file(fileDirectory);
    f.make(); // To open a channel for a specific file, this file must exist
              // Note that the file.make() method throws an EssentialServerException if it seems to be impossible to create the file
    fileChannel = null;
    randomAccessFile = null;
    try{
        randomAccessFile = new RandomAccessFile(f, "rw");
        fileChannel = randomAccessFile.getChannel();
    }catch (FileNotFoundException e){
        throw new EssentialServerException(4);
    }
    lock = null;
    while (true) {
        try{
            lock = fileChannel.tryLock();
        }catch (OverlappingFileLockException e){
            filelockingAttempts++;
            if (filelockingAttempts > maxFilelockingAttempts && maxFilelockingAttempts > -1){
                try{
                    fileChannel.close();
                    randomAccessFile.close();
                }catch (IOException ee){
                    throw new EssentialServerException(7);
                }
                throw new EssentialServerException(5);
            }
        }catch (IOException e) {
            filelockingAttempts++;
            if (filelockingAttempts > maxFilelockingAttempts && maxFilelockingAttempts > -1){
                try{
                    fileChannel.close();
                    randomAccessFile.close();
                }catch (IOException ee){
                    throw new EssentialServerException(8);
                }
                throw new EssentialServerException(6);
            }
        }
    }
}

public void release (int ExceptionType) throws EssentialServerException{
    //The int value ExceptionType indicate the EssentialServerException's type that the FileLocker class should throw if the file unlocking fails
    try{
        lock.release();
        fileChannel.close();
        randomAccessFile.close();
    }catch (IOException ee){
        throw new EssentialServerException(ExceptionType);
    }
}
}

最佳答案

直接回答你的问题 - 我不知道直接执行此操作的方法。不过,我想向您推荐AutoCloseable界面。它允许您编写可在 try-with-resources 中使用的代码。代码块。

不知道(实际上忽略)代码的所有细节,使用它可能看起来像这样:

try (FileLocker locker = new FileLocker(filename)) {
  // Use locker
}

您可以选择添加 catch 和 finally block 。但重点是,这保证了 Locker 在退出 try block 之前将被“关闭”。

此外,如果 FileLocker 只是一个任意对象,除了再次释放它之外,不会在 try {} block 中进一步使用,那么您可以简化如下:

try (new FileLocker(filename)) {
  // Your code during FileLocker
}

为了使其正常工作,您的 FileLocker 必须扩展 AutoCloseable 。实际上,如果你的FileLocker会抛出IOException,你也可以考虑扩展 Closeable反而。您将需要实现 close 方法,该方法基本上必须调用您的 release 方法。

关于java - 如何定义必须调用的方法(我自己的类的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40565573/

相关文章:

java - 处理 lambda 中的检查异常

有界队列的 Java 线程池

java - 在 Java 中从相机接收图像时出现伪影

java - 如何在android中获取连接到同一网络的设备的mac地址?

Java Streams API 的 Javascript 等价物

java - Spring 返回 String 作为 JSON,行为改变

java - 如何从android中的webview获取url中存在的值?

java - 如何使用AWS Lambda Java函数调用第三方REST API?

JAVA:从文件中读取下一行,排除所有空格

java - 依赖 p :selectOneMenu Population of values