我使用 ReentrantReadWriteLock 有什么问题?

独立

我有麻烦ReentrantReadWriteLock当我尝试擦除文件时线程挂起。我有一个计划的读取操作和最终的写入操作(当用户按下按钮时),它们使用ReentrantReadWriteLock. 下一个代码看起来不适合使用,抱歉,为了简单起见,我将所有内容放在一个地方。

public class FileDB {
    private static final String ORDERS_FILENAME = "orders.tsv";

    private ReadWriteLock ordersLock;

    private FileDB() {
        ordersLock = new ReentrantReadWriteLock();

        // Swing Timer
        ordersTimer = new Timer(0, (ActionEvent e) -> {
            readFileSplittedByTab("orders.tsv", 5, ordersLock);
        });

        ordersTimer.setDelay(5 * 1000); // 5 sec
        ordersTimer.start();
    }

    private List<String[]> readFileSplittedByTab(String filePath,
            int columns, ReadWriteLock lock) {
        lock.readLock().lock();

        File file = new File(filePath);

        // if file is absent or empty return empty list
        if (!file.exists() || file.length() == 0)
            return new ArrayList<String[]>();

        List<String> lines = null;

        try {
            lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }

        List<String[]> splittedFile = new ArrayList<>();
        String[] parts = null;

        for (String line : lines) {
            parts = line.split("\t");

            if (parts.length != columns) // skip bad string
                continue;

            splittedFile.add(parts);
        }

        return splittedFile;
    }

    private void wipeFile(String filePath, ReadWriteLock lock) {  
        PrintWriter printWriter = null;

        try {
            lock.writeLock().lock();
            Files.newBufferedWriter(Paths.get(filePath), StandardOpenOption.TRUNCATE_EXISTING).close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }
}

并像这样写操作:

wipeFile(ORDERS_FILENAME, ordersLock);

wipeFile()方法第一次触发时,一切正常。但是从第二次尝试开始,它就挂了lock.writeLock().lock()我试图wipeFile()从另一个线程调用方法,因为人们写道,写锁不应在具有读锁的一个线程中使用。

Executors.newSingleThreadExecutor().execute(() -> {
                    wipeFile(ORDERS_FILENAME, ordersLock);
});

但这无济于事,另一个线程也挂了。

所以,问题是我使用 ReentrantReadWriteLock 有什么问题?

托马斯原告

错误的是在文件被擦除(删除或文件长度等于 0)后你永远不会释放读锁:

    lock.readLock().lock();

    File file = new File(filePath);

    // if file is absent or empty return empty list
    if (!file.exists() || file.length() == 0) {
        // lock.readLock().unlock(); // this line is missing
        return new ArrayList<String[]>();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章