package com.godoro.nio.channel;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.concurrent.TimeUnit;
public class LockExample {
public static void main(String[] args) throws Exception {
File file = new File("shared.txt");
RandomAccessFile raf
= new RandomAccessFile(file, "rw");
FileChannel channel = raf.getChannel();
FileLock lock = null;
try {
lock = channel.lock();
lock = channel.tryLock();
System.out.println("Dosya kilitlendi.");
Thread.sleep(10000);
} catch (OverlappingFileLockException e) {
e.printStackTrace();
System.out.println("Çakışan dosya kilitleme yanlışlığı: " + e.getMessage());
}
if (lock != null) {
lock.release();
System.out.println("Dosya salındı.");
}
channel.close();
}
}
Dosyayı İndir