5 Replies Latest reply on Apr 12, 2011 10:40 AM by EJP

    File is deleted on Mac System even though locked using FileChannel.lock()

      Hi folks,
      I have written a utility method to delete the file if its not currently being used by other threads (using simple IO).
      To test it I have written the test case.

      In which I will spawn a thread which will use FileChannel's lock or tryLock to obtain a lock and this thread goes to sleep. Other thread will try to delete the file.And I am expecting an exception as first thread as locked it and gone to sleep.
      This scenario works fine on Windows but the same fails on Apple Mac.

      // test method...
      File file = new File(STR_TEMP_FILE_NAME);
      final FileChannel channel = new RandomAccessFile(file, "rw")

      final Thread fileLockingThread = new Thread() {
      public void run() {
      try {
      } catch (IOException e) {

      //try to delete the file and Expecting the exception here..

      tried using other approach also like
      In thread one : I will write in the file in the for loop and flush , and mean while thread 2 will try to delete the file. which again works on Windows but doesn't work on Mac.

      I am not understanding why its deleting the file on Mac? When it should not delete and throw exception?
      Any ideas? Work around? Reason?
      Is it like Apple has not implemented file locking feature ?

      Thanks in advance.