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();
}
}
}
"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();
}
}
}
- relates to
-
JDK-4490865 (fs) Cannot acquire overlapping shared locks
-
- Closed
-