**來自hdfs datanode中的使用方式...看起來相對容易,實際有幾點需要注意,
1.檔案一定要deleteonexit().
2.發生異常一定要將randoemaccessfile close()掉.
3.filelock為null時要判斷.
4.filelock關閉時,先呼叫filelock.release(), 而後filelock.chanel().close();
public class filelockdemorandomaccessfile file = new randomaccessfile(lockfile,
"rws");
filelock filelock = null;
try
file.close();
return null;
} catch (ioexception e)
file.write(managementfactory.getruntimemxbean().getname().getbytes("utf-8"));
lockfile.deleteonexit();
return filelock;
} private void
unlock(filelock filelock) throws ioexception
filelock.release();
filelock.channel().close();
} public static void
main(string args)
else
timeunit.seconds
.sleep(30);
demo.unlock(filelock);
} catch (ioexception e) catch (interruptedexception e)
}}).start();
} }
}
Java NIO 入門二 使用通道複製檔案
nio相對於io來說,增加了乙個通道的 channel public static void test3 throws exception outchannel.close inchannel.close fileoutputstream.close fileinputstream.close 首先...
php如何使用檔案鎖
fp fopen test.txt ab from the end flock fp,lock ex lock the file for waiting.fwrite fp,just a test string.start writing.flock fp,lock un release write...
linux使用flock檔案鎖
使用linux flock 檔案鎖實現任務鎖定,解決衝突 格式 flock sxun w fd flock sxon w file c command flock help flock util linux ng 2.17.2 usage flock sxun w fd flock sxon w f...