gpt4 book ai didi

java - StringBuffer 附加 - jvm 内部发生了什么

转载 作者:行者123 更新时间:2023-12-01 06:47:46 25 4
gpt4 key购买 nike

我有一个 while 循环来使用字符串缓冲区进行字符串追加。 jvm 内部发生了什么,因为追加方法是同步的,它每次都会检查对象上的锁定并在调用追加时继续。

最佳答案

没关系,只需使用 StringBuilder相反:

A mutable sequence of characters. This 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.

但是要回答您的问题,请阅读以下内容: Java Tutorial > Synchronized Methods

关于java - StringBuffer 附加 - jvm 内部发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com