learning java 文件锁

  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;

public class FileLockTest {
    public static void main(String[] args) {
        try (
            var channel = new FileOutputStream("a.txt").getChannel())
        {
            FileLock flock = channel.lock();
            Thread.sleep(100000);
            flock.release();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/lianghong881018/p/11307022.html