News for hackers. Stuff that matters.
Re:Java StringBuffer and StringBuilder performance by lmm on Monday June 13, 2016 @01:39PM (#11895664)
> How about just using StringBuilder unless you really (that 0.01% case) need thread synchronization of StringBuffer?

You wouldn't necessarily know, or notice - your users would just experience occasional random crashes. How about just using StringBuffer unless you really (that 0.01% case) need the performance of StringBuilder? As you say, profiling is the right way to approach these things.

Re:Java StringBuffer and StringBuilder performance by stickfigure on Monday June 13, 2016 @02:00PM (#11895805)
The only sane way to write multithreaded code is to assume all libraries are not thread-safe unless explicitly declared as such, and to carefully manage critical sections. There's no point in "defaulting" to threadsafe libraries; just because you use StringBuffer does not mean your code is threadsafe. If you don't understand your critical sections, you're almost certainly screwing up.
Re:Java StringBuffer and StringBuilder performance by DannyB2 on Monday June 13, 2016 @04:20PM (#11897237)
In most cases you WOULD know that StringBuilder is just fine and no thread contention exists. Most common uses of StringBuilder are where the StringBuilder is a local variable used within a single function to build up a string, and then to give you the actual string that was built up. After that the StringBuilder goes out of scope and the garbage collector will eat it up when it gets hungry. The string that StringBuilder gave you is immutable, therefore no other thread can change its value. (And hence the reason why StringBuilder is useful -- it is like a mutable string.)
Re:Java StringBuffer and StringBuilder performance by Tharkun on Monday June 13, 2016 @02:10PM (#11895896)
That's nonsense. I can't think of any realistic examples in which it would be useful for multiple threads to write to the same StringBuffer.

You might as well sprinke random and pointless synchronized blocks around your code. The magical synchronization fairy will give you a false sense of correctness.

Re:Java StringBuffer and StringBuilder performance by im3w1l on Tuesday June 14, 2016 @05:40AM (#11900828)
Some kind of logging maybe?
Re:Java StringBuffer and StringBuilder performance by Tharkun on Tuesday June 14, 2016 @10:01AM (#11902053)
What? How would that work? Why would you log to a string? Log to a file, or a network, or a pipe.
Re:Java StringBuffer and StringBuilder performance by shiv86 on Monday June 13, 2016 @03:56PM (#11896975)
99% of the time you initialize StringBuilder during a method call. Each method call stores it’s variables on the stack and each thread has its own stack. So in the above 99% usecases StringBuilder is thread safe by default.

Use cases for StringBuffer are very limited …I can’t even think of an example where I would use it.

Re:Java StringBuffer and StringBuilder performance by kodablah on Monday June 13, 2016 @01:55PM (#11895773)
> How about just using StringBuffer unless you really (that 0.01% case) need the performance of StringBuilder?

Because when writing new code you should opt-in to thread safety, not out. Collections are not concurrent by default, there is no `unsynchronized` or `non-volatile` keywords, etc. StringBuilder:StringBuffer::HashMap:ConcurrentHashMap. Also, if the performance benefit requires negligible upfront cost (e.g. providing length to ArrayList if known) you should do it and build it in your muscle memory as the default approach.

The problem is StringBuffer (unlike many other parts of the stdlib) was the only approach available for a long time so people need to change their default way of thinking. Luckily it's been a long time since 1.5 and basically everyone has changed.

Re:Java StringBuffer and StringBuilder performance by Someone on Monday June 13, 2016 @02:10PM (#11895900)
I don't think thread-safety is a big concern for string building APIs. I expect 90% or more of all uses of StringBuffer are easily verified to be used from only one thread because they allocate an instance, assign it to a local variable, append to it a few times, and finally get the string that was built exactly once.

Actually, I don't see why it would take a very smart compiler to do this kind of escape analysis and automatically and safely replace StringBuffer by StringBuilder where possible. The only large downside I can see of that is that it would require the compiler to know quite a bit of the semantics of the standard library.

If you refactor your code and pass the StringBuffer to (small) helper functions, doing this would be more work, but it would not get essentially harder.

Re:Java StringBuffer and StringBuilder performance by hyperpape on Monday June 13, 2016 @05:03PM (#11897614)
You're right about thread safety. And when you do want it, you probably want more guarantees than StringBuffer gives you (ability to synchronize multiple inserts, for instance).

With regard to the sufficiently smart compiler: it seems like you'd need every method the StringBuffer is passed to be final to do that kind of analysis.

In the case where you're writing:

    public void foo(List<String> strings) {
        StringBuffer x = new StringBuffer();
        for (String s: strings) {
            x.append(s);
        }
    }
You could conceivably do the analysis at compile time. But the whole idea behind the JIT is that you don't need to. If the code is hot, it'll get compiled, and you'll probably be fine thanks to removing all the synchronization, while if it's not hot no one cares.
Re:Java StringBuffer and StringBuilder performance by Someone on Tuesday June 14, 2016 @01:45AM (#11900032)
You're right, I forgot that all methods are implicitly virtual by default in Java.
Re:Java StringBuffer and StringBuilder performance by vardump on Tuesday June 14, 2016 @03:49AM (#11900443)
Not quite same as "virtual" in C++ though. Java methods can still be inlined at runtime.

I wish C++ had a runtime that could do same, inline smaller virtual methods, function pointers and shared library (.dll/.dylib/.so) calls at runtime.

It could also do other transformations, like optimize those branches away it can prove to be always true or false. It could flatten most abstractions, removing redundant parameter checks and call frame setups/teardowns. It could also do CPU (instruction set) specific optimizations.

I think C++ runtime that does this partial JITting is possible, but not easy. However, the benefits are interesting. While getting there won't be easy, you can achieve faster than native performance.

Something like this has already been actually done. Some examples below.

QuaC: http://digitalassets.lib.berkeley.edu/techreports/ucb/text/C...

DynamoRio: DynamoRio had some early success JITting native binaries resulting faster than native performance. Unfortunately current versions are slower than native.

https://github.com/DynamoRIO/dynamorio).

https://dspace.mit.edu/bitstream/handle/1721.1/87441/5486358...

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @03:17PM (#11896551)
As a non-java programmer, I'm wondering why you would want to opt-in to a safety system instead of out of it? My instinct would be to say that you should be safe be default, and if you find you need performance, then you verify it won't cause problems in your case and opt out of the default safety mechanism. Why is defaulting to non-safe ever the right choice?
Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @05:49PM (#11898003)
Thread-safety is implicitly different than other types of safety: There are multiple answers to the same problem, and the default one present in StringBuffer is not necessarily the one you want. For instance, StringBuffer's thread-safety only guarantees that every write will be executed without corruption. What are the possible outcomes of this executing in two threads:

    stringBuffer.append("a").append("b"); // Thread 1
    stringBuffer.append("c").append("d"); // Thread 2
The answer is, any of the 6 permutations of "abcd" such that "a" appears before "b" and "c" appears before "d". However, maybe what you really wanted was just the two permutations of "abcd" or "cdab".

Just saying, "thread-safe!" doesn't answer the question of how it's thread-safe and what its behavior will be if multiple threads are contending for it. All it means is that the program won't crash or corrupt just because multiple threads are in the same place at once, which is not really all that useful a guarantee when you're looking at very expensive locking mechanisms to get there.

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:27PM (#11898259)
I don't consider that a matter of safety, as much as data integrity. Safety, as I've defined it elsewhere within this thread, being "does not cause memory corruption or crash". If neither technique cause that, then I don't really have an opinion. If one can cause a crash in certain circumstances, and the other cannot, I don't think it's responsible to endorse the one that can cause a crash.

Does that mean you may need to figure out why your data looks weird at some later date? Possibly. But to me that much preferred to entire program failure in hard to reason about circumstances, or in memory corruption, which can lead to the same.

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @06:38PM (#11898323)
Actually, I'd rather my program just crash at the point of use than silently corrupt my data. First, my existent data will be preserved, and after fixing the issue I can rerun the operation. Second, I will get a clear crash that immediately tells me where the problem is, instead of getting garbage output that I have no idea where it sourced from. Oh, and since this is a threading problem, it's probably even transient data corruption.
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:57PM (#11898442)
That's not how threads work. Without thread safety mechanisms, you don't get occasional data corruption or the occasional crash, you get both, but often not at the same time. Occasionally data will be written out of order. Occasionally there will be enough weirdness you can have a crash. How often you get one or the other depends on the algorithms in use. Lock safe methodologies can prevent the crash, and if designed specifically for the data in question and the data structure supports it, can possibly ensure no corruption.

Given with strings you probably have data corruption no matter what, do you want crashes on top of that or not?

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @07:06PM (#11898497)
> Lock safe methodologies can prevent the crash, and if designed specifically for the data in question and the data structure supports it, can possibly ensure no corruption.

Which was the entire point of my post. "Thread-safe" does not guarantee "usage-safe", and using them interchangeably is a fallacy. If the synchronized StringBuffer does not provide the guarantees you need anyway, there's no point in paying the cost of using it.

> Given with strings you probably have data corruption no matter what, do you want crashes on top of that or not?

I just said that, yes, I would rather the program crash than silently corrupt data. I would rather it crash 100% of the time, but I'll take intermittent crashes instead. A crashed process can be restarted; it's very much harder to fix data once corruption hits. And if data corruption hits anyway, at least if I also have a crash I have an idea of where to look for the issue.

Re:Java StringBuffer and StringBuilder performance by veddan on Monday June 13, 2016 @03:48PM (#11896858)
Unless your program is designed to work that way, the fine-grained synchronization that StringBuffer offers over StringBuilder is almost certainly not very useful. And since Java is a memory-safe language, the only meaningful difference between StringBuffer and StringBuilder in "accidentally" multi-threaded code is the exact manner in which your output will garbled.
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @04:17PM (#11897215)
Up-thread, lmm, said "You wouldn't necessarily know, or notice - your users would just experience occasional random crashes." Are you saying that's incorrect?

If so, that's less an issue of safety, as much as it is data integrity, which is to my mind a less important constraint to keep. In that case, an argument about defaults with respect to safety is a non-sequitur, so I don't really have a point to argue with respect to this example (if true).

Re:Java StringBuffer and StringBuilder performance by mcguire on Monday June 13, 2016 @09:47PM (#11899203)
I think your definitions are wrong there. A crash is the best thing that can happen in the case of most, if not all errors. A program that generates bad data, or even worse, converts good data to bad data is much, much worse.
Re:Java StringBuffer and StringBuilder performance by kodablah on Monday June 13, 2016 @03:31PM (#11896696)
Because of practicality. Think of it this way, "why would you want to opt-in to better performance instead of out of it?" The main reason behind sensible defaults is that they suit most for the best benefit. Whatever language you are familiar with, I can undoubtedly show you safer yet less convenient (and less frequently needed) ways to do things to which you could pose your same question.
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @04:06PM (#11897094)
> Think of it this way, "why would you want to opt-in to better performance instead of out of it?"

Because it's unsafe some portion of the time? I don't think it's controversial to say safety should be the default, unless it imposes undue constraints. We have plenty of experience with the alternative. See C.

> The main reason behind sensible defaults is that they suit most for the best benefit.

Are we not talking about some small percentage of performance loss in string concatenation in exchange for a program that doesn't crash in the case it's used in a particular manner with threads? Am I misunderstanding this?

> Whatever language you are familiar with, I can undoubtedly show you safer yet less convenient (and less frequently needed) ways to do things to which you could pose your same question.

Yes. And when you point out where the same API exists for two constructs with different names, that achieve the same end, with small performance differences between them, and one is safe and one can possible cause the program to crash in some edge cases, I will side with the safer way as a default.

It's not like we as an industry or hobby need help writing buggy software. We've accomplished that just fine for decades.

Re:Java StringBuffer and StringBuilder performance by kodablah on Monday June 13, 2016 @04:25PM (#11897276)
> Because it's unsafe some portion of the time?

Yes, in the rare case of using string buffering across threads simultaneously. This "some" is so small as to have no practical benefits to default to the safe way. I don't know of any code that does it or would use it (and if that code did exist, you would opt in to the safety guarantees by using a different class for that rare purpose).

> Are we not talking about some small percentage of performance loss in string concatenation in exchange for a program that doesn't crash in the case it's used in a particular manner with threads? Am I misunderstanding this?

A bit. New code makes a conscious choice to be thread safe or not. I would not recommend anyone edit old code to improve performance. But for new code, thread safe string buffers are like thread safe anything else in the language, they are opt in because the default is assumed to not be thread safe unless you really mean it. All programming languages I know that support cross-thread racy invocations have a thread-unsafe-by-default approach to their stdlib. This is a reasonable approach/default for practical reasons.

> I will side with the safer way as a default

This is specifically why I used the word "practicality". In your mind I assume that there never should be such things as dynamic languages? If safety trumps all other forms of practical choice, your programming environment and library choice will be really limited. We can all theoretically tout we choose safety over everything else, but we all know we have to make practicality sacrifices for many reasons that are unrelated to safety.

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @04:35PM (#11897373)
> In your mind I assume that there never should be such things as dynamic languages?

In what way are dynamic languages "unsafe"? To be clear, what I mean here by unsafe, and everywhere I've used the word, is "can cause a crash or memory corruption", which I consider different than "not the output I expected", such as weird order of operations.

Can using one over the other cause a Java program to crash in certain circumstances? Then I advise the one with the possibility to crash not be the default, if at all possible. Is it extremely rare that it could cause a crash? Are we sure that will always remain the case? Could interesting developments later on the JVM make it markedly less rare (maybe some auto-threading loop constructs)? Perhaps we shouldn't use current common current behavior as a justification for what will be safe in the future?

Is it not a case of actual crashing or corruption, and just a non-intuitive ordering of operations than someone might expect? Well then we aren't really talking about the same thing at all, and I don't really care as much what the default should be, as a matter of industry best practices.

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @05:56PM (#11898046)
Because dynamic languages by definition don't stop you from doing silly things like:

    Math.floor("banana")
They might attempt to provide a sensible answer, such as NaN if I run the above in JavaScript. But that operation doesn't really have a sensible semantic definition and the choice to return NaN instead of crashing is somewhat arbitrary.
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:17PM (#11898198)
That expressly ignores what I set up as my criteria, so I'm unsure why you presented this.

Even if that does cause a crash, which is entirely dependent on the language, I think there's a clear delineation of whether something can be expected to crash or not based on the opt-in assumptions of the language you choose. If your language is not type-safe, you've already accepted that as a constraint ("types may not help you"). Should we expect addition to be implemented in some way that causes problems within the constraints you've already accepted? I think not.

Here's two specific examples of dynamic languages, one weakly typed (Perl), one strongly typed (Python), and how they are internally consistent.

    # perl -E 'say 1 + "2" + "four" + 8'
    11

    # python -c 'say 1 + "2" + "four" + 8'
      File "<string>", line 1
        say 1 + "2" + "four" + 8
              ^
    SyntaxError: invalid syntax
Each makes it clear what you can expect up-front. If python could occasionally crash from addition of two integers, or Perl would occasionally interpret an all alpha string as non-empty in numeric context, all for the name of being slightly faster, I would consider that the wrong thing to do as well. (Note: There are rules for how mixed alphanumeric strings are interpreted in Perl, but they are well defined).
Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @06:23PM (#11898225)
> I think there's a clear delineation of whether something can be expected to crash or not based on the opt-in assumptions of the language you choose.

I can say exactly the same thing about StringBuffer vs StringBuilder, with the obvious substitution for the word "language".

The point of kodablah's post is that, if your stance is that safety should always be chosen over other practical concerns, then a natural conclusion is that you should by default be opposed to dynamic languages, as they trade type safety for programmer comfort (for lack of a better term).

EDIT: I also want to point out that "crashing" vs throwing an error is not a distinction worth making for this conversation. If your program stops executing for any reason that could be prevented by a type-checking system, then it seems reasonable to call a type-checking system a safety feature.

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:39PM (#11898331)
> I can say exactly the same thing about StringBuffer vs StringBuilder, with the obvious substitution for the word "language".

Sure. And if you think the the difference in developer efficiency between a dynamic language and a static one is less than 10%, but when the gains are small, as has been shown in the linked article, then why pay the cost of possible program failure for a less than 10% gain in string concatenation speed when you probably won't event see it?

> I also want to point out that "crashing" vs throwing an error is not a distinction worth making for this conversation. If your program stops executing for any reason that could be prevented by a type-checking system, then it seems reasonable to call a type-checking system a safety feature.

I disagree. Exceptions which can be caught are not comparable to memory corruption, unless you can ensure the corruption can not cause a segmentation fault (which may be the case here, I have no idea).

Nobody seems to be answering my question as to what type of problem the threading error is limited to in Java. I've made clear that any objection I have to the default is moot based on possible assurances that the JVM might provide, but I haven't had anyone clarify.

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @06:50PM (#11898397)
> Sure. And if you think the the difference in developer efficiency between a dynamic language and a static one is less than 10%, but when the gains are small, as has been shown in the linked article, then why pay the cost of possible program failure for a less than 10% gain in string concatenation speed when you probably won't event see it?

I actually think that dynamic languages have negative efficiency impact over long periods of time and large numbers of developers. (If that doesn't make it clear where I stand on static vs dynamic typing...)

> Exceptions which can be caught are not comparable to memory corruption...

I never compared exceptions to memory corruption, I compared them to crashes. I could also "catch" crashes with techniques such as watch-dog processes with very similar effects. Now if you really mean "crashes" as "program faults due to memory corruption", as opposed to "unexpectedly halting execution", then you're both using a niche definition and gave a bit of deceiving definition of "unsafe":

> To be clear, what I mean here by unsafe, and everywhere I've used the word, is "can cause a crash or memory corruption"...

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @07:06PM (#11898501)
> I actually think that dynamic languages have negative efficiency impact over long periods of time and large numbers of developers.

I agree, but that does leave open the possibility of them being more efficient in some circumstances.

> I could also "catch" crashes with techniques such as watch-dog processes with very similar effects.

Without a system for handling the crash information in such a way that you can feed it back into the restarted program to retrieve prior that and recover from it, I don't really see how they are equivalent.

> Now if you really mean "crashes" as "program faults due to memory corruption", as opposed to "unexpectedly halting execution", then you're both using a niche definition and gave a bit of deceiving definition of "unsafe"

I mean either, for the entire program, not some executing branch. I think "crash" is fairly unambiguously accepted as "an unrecoverable problem within the current running context causing it to halt." An exception is not a crash, it's a problem, which if unhandled can lead to a crash. Perhaps I'm in the minority in my opinion on this, but I don't think so.

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @07:20PM (#11898564)
OK, now this is turning into a real tangent on exceptions vs crashes. I see little difference between an exception with no actionable programmatic resolution and a program crash. You can always retry, but the bottom line is that there's never going to be a good answer to flooring a banana. You can try to floor something else, or you can try to do an operation that's not flooring, but that particular combination isn't going to ever have a good solution.

> I mean either, for the entire program, not some executing branch.

That's a pretty arbitrary line to draw. The difference between one thread acting as a watch-dog for another thread, or the same situation with processes could probably be made indistinguishable with the proper setup.

Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @07:38PM (#11898670)
> I see little difference between an exception with no actionable programmatic resolution and a program crash.

What you consider actionable and what is possibly actionable may not be the same thing. For example...

> You can always retry, but the bottom line is that there's never going to be a good answer to flooring a banana.

Except potentially asking the user to supply another number, if it happened to be user input. There's an example of an actionable exception to something you seem to consider having no programmatic resolution.

> That's a pretty arbitrary line to draw.

Not according to wikipedia. The following is under the Application Crashes subsection of Crash (Computing)[1]

An application typically crashes when it performs an operation which is not allowed by the operating system. The operating system then triggers an exception or signal in the application. Unix applications traditionally responded to the signal by dumping core. Most Windows and Unix GUI applications respond by displaying a dialogue box (such as the one shown to the right) with the option to attach a debugger if one is installed. This behavior is called "crashing". Some applications attempt to recover from the error and continue running instead of crashing.

Note the last sentence, which implies recovery is not crashing. But you can call bullshit on Wikipedia as source (because that's sometimes valid), I don't really care. This whole discussion has devolved to the point it's not useful to continue. You stated your preference to crash elsewhere, which I have a difference in opinion on, so I don't think any headway will be made there. We can just agree to disagree on what we think is a best default behavior for a language.

1: https://en.wikipedia.org/wiki/Crash_(computing)

Re:Java StringBuffer and StringBuilder performance by dragonwriter on Monday June 13, 2016 @06:03PM (#11898098)
I don't know, treating anything that is not a number as NaN (and mathematical operations on it as producing NaN) seems to be exactly semantically correct.

I mean, really, all NaN is in any language is a signal that a mathematical operation was used with a set of inputs for which the operation does not produce a defined numerical result. In a sense, this is always a sign of an insufficiently expressive type system that can't prevent you from feeding a bad value (or bad combination of values) to the operation.

Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @06:10PM (#11898141)
I said it was a sensible response to a non-sensible operation. The operation of flooring "banana" is not semantically-correct. The fact that IEEE floating point numbers happens to have a value that provides a sensible result to this operation is a bit of a fluke. What if the return value was an integer type, which is perfectly valid considering that the floor operation always provides an integer result?
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:22PM (#11898220)
What is sensible is first based on the language definition, not your opinion. If the language defines it explicitly and has well defined rules, there's nothing non-sensible about it. Consider the following, which fall well within the definition of how Perl handles implicit conversion of strings to numeric types, and is well known to Perl programmers:

    # perl -E 'say "1zero"+0 '
    1
    # perl -E 'say "zero1"+0 '
    0
Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @06:32PM (#11898289)
No, I'm pretty sure that the question "what's the mathematical floor of banana" doesn't make any sense, no matter what the language definition is. The fact that some (dynamic) languages even allow you to ask the question is the entire point being made by kodablah in regards to typing as a safety mechanism. The fact that some smaller subset of languages even attempt (and fail) to make sense of the operation (weak typing) is just diving straight off the deep-end of safety.
Re:Java StringBuffer and StringBuilder performance by kbenson on Monday June 13, 2016 @06:49PM (#11898394)
I'm not sure there's a point in continuing this when I've defined, multiple times, the definition of safety I'm working under for the purposes of this discussion and my point, and you willfully ignore it to argue something entirely different time and again. I'm not interesting in discussing the merits of dynamic vs static languages, nor weak vs strong typing, when this started out as if you have a language feature that might cause a crash under normal, expected allowed behavior within the language, and one that will not cause a crash a a very small performance draw for that specific operation, I think defaulting to the non-crashing operation is the correct choice. Feel free to speak to that point if you would like to continue, because I don't see how the current tangent applies.
Re:Java StringBuffer and StringBuilder performance by jdmichal on Monday June 13, 2016 @07:01PM (#11898462)
The entire point of this "tangent" (which it isn't) is that the same reason you would make the choice to use the "unsafe" variant are the same reasons you would argue to not use a type-safe language. Or, rather, if you're default inclination is to always choose safety over other factors, then why not apply the same logic to languages? The fact is that you seem to be drawing an arbitrary line at language-use in your logic here.
Re:Java StringBuffer and StringBuilder performance by mcguire on Monday June 13, 2016 @09:31PM (#11899153)
StringBuffer doesn't actually give you any useful safety guarantees. If your program has threading bugs, a mangled string builder is probably the least of the problems.

Thread safety isn't something you build up from tread safe components. It has to be designed in from the top.

Further, almost all string builder uses are trivially thread safe.

Re:Java StringBuffer and StringBuilder performance by vardump on Monday June 13, 2016 @01:53PM (#11895757)
Are you playing a devil's advocate?

I just don't think this is the right place to do the synchronization. Wouldn't a Double box (that's really a "SynchronizedDouble") be silly?

Besides, can you think of any legitimate reason to concurrently append to a StringBuffer in the first place?