// file locking using JDK 1.2 version of java.io.File. // This example from the book "Just Java 1.2" page 608-610 import java.io.*; public class exclude { public static void main(String a[]) throws Exception { File locker = new File("/tmp/mailfile.lock"); boolean IgotIt = false; AcquireLockLoop: for ( ; ; ) { // repeat forever IgotIt = locker.createNewFile(); if (IgotIt) { // we created the file (got the lock) break AcquireLockLoop; } else { // otherwise, sleep for a bit and try again System.out.println(a[0] + " didn't get file, trying again"); Thread.sleep(3000); } } if (IgotIt) { // do regular work here, // assured that we have sole access to the resource. System.out.println(a[0]+ " got the file!"); locker.deleteOnExit(); Thread.sleep(2000); } } }