[java] stringbuffer optimization

Started by
0 comments, last by sma202 23 years, 3 months ago
Heres something I was wondering about, suppose we have the following statement: StringBuffer sb = new StringBuffer() sb.append(a); sb.append(b); sb.append(c); And contrast it with this code: StringBuffer sb = new StringBuffer() synchronized (sb) { sb.append(a); sb.append(b); sb.append(c); } Is the second piece of code faster because the first code has to perform 3 internal locks within the stringbuffer class versus only one in second piece of code?
Advertisement
I would be interested in the answer to this. But unless you need the string concat "+" operation I would suggest making your own string class that does not check for sync, assuming you don''t need it. I assume that even if it has a lock from the start, the stringbuffer will still check for a lock.

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson

This topic is closed to new replies.

Advertisement