2 Questions on StringBuffer

Ulf Zibis Ulf.Zibis at gmx.de
Wed Mar 17 17:01:08 UTC 2010


Am 17.03.2010 17:36, schrieb Rémi Forax:
> Le 17/03/2010 17:29, Ulf Zibis a écrit :
>> Why there are 2 methods which do not use the super method, where I 
>> can't see any difference? :
>>
>>     public synchronized char charAt(int index)
>>     public synchronized void setCharAt(int index, char ch)
>>
>> Wouldn't ensureCapacity better coded as follows? :
>>     public void ensureCapacity(int minimumCapacity) {
>>         if (minimumCapacity > value.length) synchronized {
>>             ensureCapacity(minimumCapacity);
>>         }
>>     }
>> This would save the synchronization if there is nothing to do.
>>
>> -Ulf
>>
>>
>>
>
> no, it doesn't work.
> if some variables are not in the synchronized block,
> they can be updated by one thread but this change will be not visible 
> in another thread.

Hm, those values are checked again in the super.ensureCapacity(), so 
inside the synchronized block.

I guess this is the answer on my 2nd question, thanks.
Please excuse little typo, I meant:
             super.ensureCapacity(minimumCapacity);

-Ulf








More information about the core-libs-dev mailing list