(fs) Cannot acquire overlapping shared locks

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P2
    • None
    • Affects Version/s: 1.4.0
    • Component/s: core-libs
    • sparc
    • solaris_8

      Cannot acquire overlapping shared locks.

      Specification for FileLock says "A shared lock prevents other concurrently-running programs from acquiring an overlapping exclusive lock,
      but does allow them to acquire overlapping shared locks."
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      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);
                 FileLock fl = c.lock(0,c.size(),true);
                 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(){
           System.out.println("T: Run started");
           FileLock flock = null;
           try {
             flock = fc.lock(0,fc.size(),true);
             System.out.println("T: TEST PASS. Obtained file lock . "+flock);
           } catch(OverlappingFileLockException e){
             result[0] = false;
             System.out.println("T: TEST FAIL.could not acquire shared lock on overlaping shared lock.");
           }
           catch(Exception e){
             result[0] = false;
             e.printStackTrace();
           }
        }
      }


      Same is the case when tryLock is used instead of lock().

            Assignee:
            Mark Reinhold
            Reporter:
            J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: