Friday, February 29, 2008

Native file locks in java

Java 1.4 introduced the native io layer into the JDK. One of the nice things you can do with it is to execute a native file lock that gets acknowledged by both “fcntl”- and “flock”-style locking. This is tremendously helpful if you need to share resources with native programs. So what is in C


int fd = open("/path/to/file", O_RDWR);

if (flock(fd,LOCK_EX) != 0 ) { ... }

printf("locked file\\npress return");
char c = getchar();

if (flock(fd,LOCK_UN) != 0 ) { ... }

printf("released file\\n");
close(fd);


int fd = open("/path/to/file", O_RDWR);

struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = 0;

if (fcntl(fd, F_SETLK, &lock) == -1) { ... }

printf("locked file\\npress return");
char c = getchar();

lock.l_type = F_UNLCK;

if (fcntl(fd, F_SETLK, &lock) == -1) { ... }

printf("released file\\n");
close(fd);

becomes this in java


File file = new File("/path/to/file");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock();

System.out.println("locked file\\npress return");
System.in.read();

lock.release();
System.out.println("released file\\n");

No comments: