gpt4 book ai didi

java - Java 中的 FileLock 在同一进程内或不同进程之间或两者之间跨多个线程是否安全?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:53:37 24 4
gpt4 key购买 nike

Java 中的 FileLock 在同一进程内或不同进程之间或两者之间的多个线程之间是否安全?

javadoc说:

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

File-lock objects are safe for use by multiple concurrent threads.

我不太明白:这是否意味着 FileLock 在同一个进程内的多个线程、多个不同进程之间或两者之间是安全的?

最佳答案

再往下:

This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.

这强烈表明它的目的是在不同进程之间使用。

这一行:

They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

建议如果您有一个包含多个线程的进程,则不应使用它。在这种情况下,您可以改为使用 synchronized 部分或 ReadWriteLock

关于java - Java 中的 FileLock 在同一进程内或不同进程之间或两者之间跨多个线程是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10531286/

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