Re: synchronized using String.intern()
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Fri, 30 Jan 2009 23:15:32 +0000
On Fri, 30 Jan 2009, Paul J. Lucas wrote:
Zig wrote:It will return the same objects for two threads within the same VM. It would obviously not lock the file from access by another process, including another VM.
Yes, I know (and I don't care about other VMs or processes).
Also, I think current VMs implement String.intern by adding unique values to the Permanent Generation heap? Thus, the result of String.intern does not get garbage collected. Using String.intern for many temporary strings has been reported to lead to OutOfMemory errors.
In my case, the number of such strings is guaranteed to be on the order of 1-10, so, again, I don't care.
I think i'd be looking for a way to refactor the architecture so that all access to one of those files went through a single application object. I would then either synchronize on that, or site a higher-level locking mechanism there.
Something like:
class ApplicationFile {
private File file;
private ReadWriteLock lock;
public void addEntryToFile(Entry entry) {
if (!lock.writeLock().tryLock(200, MILLISECONDS))
throw new ApplicationException("timed out waiting to lock file " + file.getName());
try {
ADD ENTRY TO FILE GIVING FILE
}
finally {
lock.unlock();
}
}
public void doSomeOtherLowLevelOperationOfYourApplication() {
// similar to the above
}
// and more methods for the other low-level ops
}
I'd then stick the 1-10 of these that get used in a HashMap, and pull them out and use them as needed.
Even better, rather than passing around strings in the app to identify which file needs to be worked on, i'd pass references to the ApplicationFile instances, to save on the lookup.
tom
--
Only men's minds could have mapped into abstraction such a territory
.
- Follow-Ups:
- Re: synchronized using String.intern()
- From: Paul J. Lucas
- Re: synchronized using String.intern()
- References:
- synchronized using String.intern()
- From: Paul J. Lucas
- Re: synchronized using String.intern()
- From: Zig
- Re: synchronized using String.intern()
- From: Paul J. Lucas
- synchronized using String.intern()
- Prev by Date: Re: K-D tree or something similar
- Next by Date: Re: list all files on classpath
- Previous by thread: Re: synchronized using String.intern()
- Next by thread: Re: synchronized using String.intern()
- Index(es):
Relevant Pages
|