gpt4 book ai didi

java - ReentrantReadWriteLock 上的读锁是否足以并发读取 RandomAccessFile

转载 作者:搜寻专家 更新时间:2023-11-01 01:39:12 25 4
gpt4 key购买 nike

我正在写一些东西来处理对数据库文件的并发读/写请求。

ReentrantReadWriteLock看起来很般配。如果所有线程都访问一个共享的 RandomAccessFile对象,我是否需要担心并发读者的文件指针?考虑这个例子:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Database {

private static final int RECORD_SIZE = 50;
private static Database instance = null;

private ReentrantReadWriteLock lock;
private RandomAccessFile database;

private Database() {
lock = new ReentrantReadWriteLock();

try {
database = new RandomAccessFile("foo.db", "rwd");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
};

public static synchronized Database getInstance() {
if(instance == null) {
instance = new Database();
}
return instance;
}

public byte[] getRecord(int n) {
byte[] data = new byte[RECORD_SIZE];
try {
// Begin critical section
lock.readLock().lock();
database.seek(RECORD_SIZE*n);
database.readFully(data);
lock.readLock().unlock();
// End critical section
} catch (IOException e) {
e.printStackTrace();
}
return data;
}

}

在 getRecord() 方法中,多个并发读取器是否可以进行以下交错?

Thread 1 -> getRecord(0)
Thread 2 -> getRecord(1)
Thread 1 -> acquires shared lock
Thread 2 -> acquires shared lock
Thread 1 -> seeks to record 0
Thread 2 -> seeks to record 1
Thread 1 -> reads record at file pointer (1)
Thread 2 -> reads record at file pointer (1)

如果使用 ReentrantReadWriteLock 和 RandomAccessFile 确实存在潜在的并发问题,那么替代方案是什么?

最佳答案

这是一个锁定文件和解锁文件的示例程序。

try { // Get a file channel for the file 

File file = new File("filename");

FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Use the file channel to create a lock on the file.

// This method blocks until it can retrieve the lock.

FileLock lock = channel.lock(); // Try acquiring the lock without blocking. This method returns // null or throws an exception if the file is already locked.

try {

lock = channel.tryLock();

} catch (OverlappingFileLockException e){}


lock.release(); // Close the file

channel.close();
}

catch (Exception e) { }

关于java - ReentrantReadWriteLock 上的读锁是否足以并发读取 RandomAccessFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1587218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com