Re: synchronized using String.intern()
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Fri, 30 Jan 2009 13:13:27 -0800
Paul J. Lucas wrote:
I've seen differing opinions on whether it will do as one would expect to do something like:
synchronized ( myString.intern() ) {
// ...
}
that is: for a given, unique string (say "foo"), does String.intern() guarantee that:
s.intern() == t.intern() iff s.equals( t )
assuming s != null && t != null? I.e., does it guarantee the same object for a string composed of the same characters? If yes, then synchronizing on it should work as one would expect, right?
The use case is to prevent concurrent access to a particular file, e.g.:
File f = ...;
synchoronzed ( f.getCanonicalPath().intern() ) {
// ...
}
If this will *not* work, why not? And how can I achieve what I want?
- Paul
I'm not at all sure "synchronized" is the right way to deal with
preventing concurrent access to a file. It has no time out, and requires
all work from open through close to be done during execution of a single
block. It seems to me to be best used for short pieces of computation.
If file locking does not do what you want, it may be better to use e.g.
a java.util.concurrent.Semaphore. You could use a Map to associate one
with each canonical File object.
Patricia
.
- References:
- synchronized using String.intern()
- From: Paul J. Lucas
- synchronized using String.intern()
- Prev by Date: Re: Reverse File Reader developed: I need to bufferize it !
- Next by Date: Re: The yearning for a preprocessor
- Previous by thread: Re: synchronized using String.intern()
- Next by thread: Re: synchronized using String.intern()
- Index(es):
Relevant Pages
|