Redian新闻
>
Re: How to lock a file and detect a thread is over?
avatar
Re: How to lock a file and detect a thread is over?# Java - 爪哇娇娃
E*l
1

Usually what you should do for situation like this is to
design a class with mutually exclusive functions (use
synchronized prefix). All the file access should be implemented
in this class. In this way when a thread is doing something
to the file, no other thread can access the file. This may
not be what you want if you want to allow multiple threads
to read the file at the same time though. You can follow
the logic and design a class which allows multiple reads but
no read when writing is goin
avatar
o*d
2

I guess ditto try to prevent other program, instead of other
threads from accessing the file when it is written. Is it
right, ditto?
Synchronized method can claim lock on the object, but that
lock is only valid to the thread within the same
application.
My solution is to create a temp file in the system temp
directory. After finished writing, rename it.
I don't like global variable and there is no such thing in
Java( Probably you can simulate it). Besides, this solution
only works when the to-b

【在 E*****l 的大作中提到】
:
: Usually what you should do for situation like this is to
: design a class with mutually exclusive functions (use
: synchronized prefix). All the file access should be implemented
: in this class. In this way when a thread is doing something
: to the file, no other thread can access the file. This may
: not be what you want if you want to allow multiple threads
: to read the file at the same time though. You can follow
: the logic and design a class which allows multiple reads but
: no read when writing is goin

avatar
m*t
3

This one suffers the same pitfall that you just pointed out
for his solution to the second problem. 8-)
Whether there is a satisfying answer to ditto's question relying
on whether he needs a cooperative or mandatory locking.
Here is some "strategy guide" from Sun (go to question 6):
http://java.sun.com/people/linden/faq_b2.html

【在 o**d 的大作中提到】
:
: I guess ditto try to prevent other program, instead of other
: threads from accessing the file when it is written. Is it
: right, ditto?
: Synchronized method can claim lock on the object, but that
: lock is only valid to the thread within the same
: application.
: My solution is to create a temp file in the system temp
: directory. After finished writing, rename it.
: I don't like global variable and there is no such thing in

avatar
m*t
4

Hmmm what I was trying to say is to use a temp file to "flag"
the locking status would have the same drawback as to use a global
variable to check whether a thread is still alive. That is,
if the program currently holding the lock fails to delete the
"flag file" before it exits, there will be deadlock.

【在 o**d 的大作中提到】
:
: I guess ditto try to prevent other program, instead of other
: threads from accessing the file when it is written. Is it
: right, ditto?
: Synchronized method can claim lock on the object, but that
: lock is only valid to the thread within the same
: application.
: My solution is to create a temp file in the system temp
: directory. After finished writing, rename it.
: I don't like global variable and there is no such thing in

avatar
o*d
5

I see what you mean. But I am not saying using the temp
file as a "flag". Even you use something as a flag, you can
not prevent some other application from deleting it. That
flag is still within your application. Other
application/program will not check your flag first.
The original quesiton is how to "prevent other programs from
touching it, even read it".
My solution doesn't use flag. The temp file is the file we
will write the real content to. Making it a temp file in the
system temp dir an

【在 m******t 的大作中提到】
:
: Hmmm what I was trying to say is to use a temp file to "flag"
: the locking status would have the same drawback as to use a global
: variable to check whether a thread is still alive. That is,
: if the program currently holding the lock fails to delete the
: "flag file" before it exits, there will be deadlock.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。