java - Acquire a lock on a file -


i want acqeuire lock on file when threo read gets started on specific file ,so no other application can read file has been locked , want release lock file when thread terminates.

you can acquire filelock via filechannel. obtain filechannel:

in release file channel can obtained existing fileinputstream, fileoutputstream, or randomaccessfile object invoking object's getchannel method, returns file channel connected same underlying file.

however, java doesn't have control on type of file locking os offers , therefore recommendation of api use lock if advisory file lock.

whether or not lock prevents program accessing content of locked region system-dependent , therefore unspecified. native file-locking facilities of systems merely advisory, meaning programs must cooperatively observe known locking protocol in order guarantee data integrity. on other systems native file locks mandatory, meaning if 1 program locks region of file other programs prevented accessing region in way violate lock. on yet other systems, whether native file locks advisory or mandatory configurable on per-file basis. ensure consistent , correct behavior across platforms, recommended locks provided api used if advisory locks.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -