Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4490521

(fs) tryLock does not return null for overlapping lock acquired by other thread

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs
    • sparc
    • solaris_8

      tryLock(long,long,boolean) for an overlapping region (already locked by another thread) does not return null value whereas specification for return value of tryLock(long,long,boolean) in FileChannel class says:
      "Returns: ...., or null if the lock could not be acquired because another program holds an overlapping lock."

      import java.io.*;
      import java.nio.channels.*;

      public class FCLock {
          public static void main(String[] args) throws Exception {
              FCLock tests = new FCLock();
              tests.lockTest();
           }
          public boolean lockTest() throws Exception {
              boolean[] bReturn = {true};
              RandomAccessFile raf = null;
              FileChannel c = null;
              File tempFile = new File("blahTemp");
              tempFile.deleteOnExit();
              initTestFile(tempFile);
              try {
                 raf = new RandomAccessFile(tempFile,"rw");
                 c = raf.getChannel();
                 T t = new T(c,bReturn);
                 long midSize = c.size()/2;
                 FileLock fl = c.lock(0,midSize,false);
                 System.out.println("Main Thread: Obtained file lock."+fl.toString());
                 t.start();
                 while(t.isAlive()) ; //Wait till t is dead.
              } catch (Exception e) {
                 bReturn[0] = false;
                 e.printStackTrace();
              }
              return bReturn[0];
          }

         //Creates file blah:
         private void initTestFile(File blah) throws Exception {
              FileOutputStream fos = new FileOutputStream(blah);
              BufferedWriter awriter
                  = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
              for(int i=0; i<4000; i++) {
                  String number = new Integer(i).toString();
                  for (int h=0; h<4-number.length(); h++)
                      awriter.write("0");
                  awriter.write(""+i);
                  awriter.newLine();
              }
             awriter.flush();
             awriter.close();
          }
      } // Class FCLock

      class T extends Thread {
         FileChannel fc;
         boolean[] result;
         public T(FileChannel c,boolean[] result){
           this.fc = c;
           this.result = result;
         }
         public void run(){
           result[0] = true;
           System.out.println("T: Run started");
           FileLock flock = null;
           long midSize = 0;
           //{ This block is written just so flock contains some value other than null
           try {
             midSize = fc.size()/2;
             flock = fc.lock(midSize,fc.size(),false);
             System.out.println("T: Obtained file lock 1. "+flock);
           } catch(Exception ex){
             result[0] = false;
             ex.printStackTrace();
           }
           // } Now flock contains some value other than null.
           System.out.println("T: Trying to overwrite the file lock value");
           try {
             //lock on this region has already been acquired by main thread.
             flock = fc.tryLock(0,midSize,false);
             System.out.println("T: Obtained file lock 2. "+flock);
           } catch(OverlappingFileLockException e){
             if(flock != null) {
                result[0] = false;
                System.out.println("T: TEST FAIL.tryLock() does not returns null value for already locked region.");
                System.out.println("T: fileLock vaiable holds this value: "+flock);
              }
              else {
                System.out.println("T: TEST PASS.tryLock() returns null value for already locked region.");
              }
           }
           catch(Exception e){
             result[0] = false;
             e.printStackTrace();
           }

        }
      }


            mr Mark Reinhold
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: