[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: StringBuilder
From: |
Thomas Zander |
Subject: |
Re: StringBuilder |
Date: |
Fri, 17 Dec 2004 14:59:38 +0100 |
User-agent: |
KMail/1.7.91 |
On Friday 17 December 2004 13:50, Jeroen Frijters wrote:
> Hi,
>
> Attached is an example of how StringBuilder could be made thread safe
> and still enable sharing the char[]. Unfortunately it requires
> allocating an extra temp object and JIT magic, but maybe someone is
> interested in pursuing this (I'm not because I have to special case
> StringBuilder in another way).
>
> Note that you could also get rid of the temporary object by using a
> String instance for this, but that would require making the value and
> count fields of String non-final and this has memory model consequences
> as well (thereby making String non-portable).
Interresting concept;
several bugs:
1) doing an aquire can return a null; meaning the toString() can cause
nullpointer exceptions.
2) the release should be synhronized.
Hmm; there are several other bugs which have to do with threading..
A better solution would be have the complete implementation of append() and
toString() in a synchronized block on a lock object.
So:
private final Object lock = new Object();
public String toString() {
synchronized(lock) { /* do stuff here */ }
}
--
Thomas Zander
pgp8SLsG3Q3Bz.pgp
Description: PGP signature