java “StringBuffer 是同步的(或线程安全的)而 StringBuilder 不是”,为什么这会使 StringBuffer 方法变慢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6293968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"StringBuffer is synchronized (or thread-safe) and StringBuilder is not", why does this make StringBuffer methods slower?
提问by Shailesh Tainwala
After reading this - What does 'synchronized' mean?I was still unable to understand why StringBuffer would be slower than StringBuilder in a thread-safe environment. What extra time-consuming work does StringBuffer have to do that makes it slower?
阅读本文后 - “同步”是什么意思?我仍然无法理解为什么在线程安全环境中 StringBuffer 会比 StringBuilder 慢。StringBuffer 必须做哪些额外的耗时工作使其变慢?
回答by Mike Samuel
There is some small overhead acquiring and releasing even an uncontended lock, and lock elisionwon't work in StringBuffer
even if most instances are not used cross-thread because an instance could be.
即使是无竞争的锁,获取和释放也有一些小开销,即使大多数实例没有跨线程使用,锁省略也不起作用,StringBuffer
因为实例可能是。
See http://book.javanb.com/java-threads-3rd/jthreads3-CHP-5-SECT-1.htmlfor a description of what the VM has to do when acquiring and releasing locks.
请参阅http://book.javanb.com/java-threads-3rd/jthreads3-CHP-5-SECT-1.html,了解 VM 在获取和释放锁时必须执行的操作。
回答by Atreys
Making sure things are running in synch. Or, more to the point, out of synch. Synchronizing a method call means two different invocations of that method (on that object if it is not static) have to take turns entering the method. Thread B cannot enter method synchMeth until Thread A (already in the method) has finished.
确保事情同步运行。或者,更重要的是,不同步。同步方法调用意味着该方法的两次不同调用(在该对象上,如果它不是静态的)必须轮流进入该方法。在线程 A(已在方法中)完成之前,线程 B 无法进入方法 synchMeth。
The checks for whether a synchronized block has been locked or not, and by whom, take extra time.
检查同步块是否已被锁定以及由谁锁定需要额外的时间。
回答by sgokhales
Read this from JavaDoc
从JavaDoc阅读此内容
StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
StringBuilder 类提供与 StringBuffer 兼容的 API,但不保证同步。此类旨在用作 StringBuffer 的替代品,用于在单个线程使用字符串缓冲区的地方(通常是这种情况)。在可能的情况下,建议优先使用此类而不是 StringBuffer,因为在大多数实现下它会更快。
You must read this article on StringBuffer vs. StringBuilder performance comparison
您必须阅读有关StringBuffer 与 StringBuilder 性能比较的这篇文章
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, {StringBuilder}. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
从 JDK 5 版本开始,这个类已经补充了一个设计用于单线程的等效类,{StringBuilder}。通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但速度更快,因为它不执行同步。
Additional Useful Link : Difference between StringBuffer and StringBuilder class.