2 ms·
I don't know if that's the answer you're looking for, but a call to append is synchronized: public synchronized StringBuffer append(/* Something */); ...
by AYBABTME 14y ago
I don't know if that's the answer you're looking for, but a
call to append is synchronized:
public synchronized StringBuffer append(/* Something */);
... but chaining them doesn't mean that the chain is synchronized.
sb.append(key).append("=").append(value).append(", ");
is executed the same way as
sb.append(key);
sb.append("=");
sb.append(value);
sb.append(", ");
Two concurrent threads running this part of code might do this:
sb.append(key); // # 1
sb.append("="); // # 2
sb.append(value); // # 3
sb.append(", "); // # 4
// Thread 1 executes #1
// Thread 2 executes #1
// Thread 1 executes #2
// Thread 1 executes #3
// Thread 2 executes #2
// Thread 2 executes #3
// Thread 2 executes #4
// Thread 1 executes #4
This can be executed in many more different ways.
To ensure that only one thread executes those `append` at once, you would have to synchronize on the `StringBuffer` instance:
synchronize(sb){
sb.append(key).append("=").append(value).append(", ");
}
Then only one thread will do the appending at a time. But this introduce yet another lock in your code, and render the `synchronized` methods useless and redondant.
- Cloven 14y agoit introduces a lock in your code, because that is what you want -- you want the string to be built by one thread only. Why the strange comments about 'yet another lock' and the synchronized methods being 'useless' or 'redondant' (sic)?
- spacemanaki 14y agoThanks, I looked up the source and saw that append was synchronized too. I was mostly asking about this claim from the OP: "the JIT and JVM will attempt to hold onto the lock between calls in the interests of efficiency". I don't think that's true.