由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Re: How to lock a file and detect a thread is over?
相关主题
Java synchronized method一问java ,wait ,notify notifyall
multi-threading guru们 (转载)How to make code thread-safe?应该怎么答?
新手问个multi-threading关于synchronized和volatile的问题[转载] 现在还有什么OS不是THREAD级调度的吗?
Apply lock on a class.java thread question
问一个基础问题thread independent on a single-cpu machine?
请教:怎么把synchronize的method改成用thread 但thread safe呢?Thread对应的input和output问题
java 程序中的实时控制多少个thread 就算不正常?
如何让两个socket并行执行thread怎么学multithreading/concurrency?
相关话题的讨论汇总
话题: file话题: thread话题: lock话题: temp话题: flag
进入Java版参与讨论
1 (共1页)
E*****l
发帖数: 2662
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
o**d
发帖数: 11
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

m******t
发帖数: 2416
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

m******t
发帖数: 2416
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

o**d
发帖数: 11
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.

1 (共1页)
进入Java版参与讨论
相关主题
怎么学multithreading/concurrency?问一个基础问题
one multi-threading question请教:怎么把synchronize的method改成用thread 但thread safe呢?
Re: native thread 和green threadjava 程序中的实时控制
Re: Client-Server and actionPerformed如何让两个socket并行执行thread
Java synchronized method一问java ,wait ,notify notifyall
multi-threading guru们 (转载)How to make code thread-safe?应该怎么答?
新手问个multi-threading关于synchronized和volatile的问题[转载] 现在还有什么OS不是THREAD级调度的吗?
Apply lock on a class.java thread question
相关话题的讨论汇总
话题: file话题: thread话题: lock话题: temp话题: flag