• Post Reply Bookmark Topic Watch Topic
  • New Topic

File lock doesn't prevent threads writing to the file  RSS feed

 
K Zhang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I wonder if someone could help me understand the behavior of Java FileLock.
I have one java program to lock the file:


And I have another program writing to the same file


When I run the first program in 2 java threads. The thread that started later would throw an exception, which is as expected, because file is already locked exclusively.
However, when I ran the second program immediately after the first program was started, the string was still written to the file, while the file was supposed to be locked exclusively.
Is it an expected behavior?
Is there any way in Java to acquire the file lock and prevent any application from writing into it?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging from the file path, you're probably using Linux or UNIX. Linux and UNIX don't have the file locking mechanism that Windows has; they allow you to do anything with any file, even if it's already in use by another process. That also includes completely deleting the file. I think that if you tried the same code on Windows that you would get an exception.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how you are doing it however when I tried the same I got following exception:

Exception in thread "main" java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at java.io.FileOutputStream.write(Native Method)
at OutputFileStreamTest.main(OutputFileStreamTest.java:11)
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob yes I tried the same on both windows and unix...it is giving exception on windows whereas allowing to write in unix.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why java API does not contan these limitations?

Should they really contain?
 
K Zhang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Thank you for your replies.
Yes I'm using red hat enterprise 5.
I also did some research that this Java FileLock is Advisory locking, which doesn't guarantee the real file lock.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john sal wrote:Why java API does not contan these limitations?

Should they really contain?

It is already documented:
Platform dependencies

This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.

Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified. The native file-locking facilities of some systems are merely advisory, meaning that programs must cooperatively observe a known locking protocol in order to guarantee data integrity. On other systems native file locks are mandatory, meaning that if one program locks a region of a file then other programs are actually prevented from accessing that region in a way that would violate the lock. On yet other systems, whether native file locks are advisory or mandatory is configurable on a per-file basis. To ensure consistent and correct behavior across platforms, it is strongly recommended that the locks provided by this API be used as if they were advisory locks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!