简体   繁体   English

什么是使用StringBuffer的真实示例?

[英]What's a real-world example of using StringBuffer?

I'm using Java 6. 我正在使用Java 6。

I've only written a couple of multi-threaded applications so I've never encountered a time when I had several threads accessing the same StringBuffer. 我只写了几个多线程应用程序,所以我从来没有遇到过多个线程访问同一个StringBuffer的时候。

Could somebody give me a real world example when StringBuffer might be useful? 当StringBuffer可能有用时,有人会给我一个真实世界的例子吗?

Thanks. 谢谢。

EDIT: Sorry I think I wasn't clear enough. 编辑:对不起我觉得我不够清楚。 I always use StringBuilder because in my applications, only one thread accesses the string at a time. 我总是使用StringBuilder,因为在我的应用程序中,一次只有一个线程访问字符串。 So I was wondering what kind of scenario would require multiple threads to access StringBuffer at the same time. 所以我想知道什么样的场景需要多个线程同时访问StringBuffer。

The only real world example I can think of is if you are targetting Java versions befere 1.5. 我能想到的唯一现实世界的例子是,如果你的目标是Java版本的1.5。 The StringBuilder class was introduced in 1.5 so for older versions you have to use StringBuffer instead. StringBuilder类是在1.5中引入的,因此对于旧版本,您必须使用StringBuffer

In most other cases StringBuilder should be prefered to StringBuffer for performance reasons - the extra thread safety provided by StringBuffer is rarely required. 在大多数其他情况下,出于性能原因, StringBuilder应首选StringBuffer - 很少需要StringBuffer提供的额外线程安全性。 I can't think of any obvious situations where a StringBuffer would make more sense. 我想不出StringBuffer更有意义的任何明显情况。 Perhaps there are some, but I can't think of one right now. 也许有一些,但我现在想不到一个。

In fact it seems that even the Java library authors admit that StringBuffer was a mistake : 事实上,即使是Java库作者也承认StringBuffer是一个错误

Evaluation by the libraries team: 图书馆团队评估:

It is by design that StringBuffer and StringBuilder share no common public supertype. 根据设计,StringBuffer和StringBuilder不共享公共超类型。 They are not intended to be alternatives: one is a mistake (StringBuffer), and the other (StringBuilder) is its replacement. 它们不是替代品:一个是错误(StringBuffer),另一个(StringBuilder)是它的替代品。

If StringBuilder had been added to the library first StringBuffer would probably never have been added. 如果已将StringBuilder添加到库中,则可能永远不会添加StringBuffer If you are in the situation that multiple threads appending to the same string seems like a good idea you can easily get thread safety by synchronizing access to a StringBuilder . 如果您处于附加到同一字符串的多个线程似乎是个好主意的情况下,您可以通过同步对StringBuilder访问来轻松获得线程安全性。 There's no need for a whole extra class and all the confusion it causes. 没有必要增加一个额外的课程以及它造成的所有混乱。

It also might be worth noting that the .NET base class library which is heavily inspired by Java's libraries has a StringBuilder class but no StringBuffer and I've never seen anyone complaining about that. 值得注意的是,受Java库强烈启发的.NET基类库有一个StringBuilder类但没有StringBuffer ,我从未见过有人抱怨过这个问题。

当您拥有一个日志文件并且多个线程正在记录错误或警告并写入该日志文件时,一个简单的案例就是。

In general, these types of buffered string objects are useful when you are dynamically building strings. 通常,在动态构建字符串时,这些类型的缓冲字符串对象非常有用。 They attempt to minimize the amount of memory allocation and deallocation that is created when you continually append strings of a fixed size together. 它们尝试最小化在连续追加固定大小的字符串时创建的内存分配和释放的数量。

So a real world example, imagine you are manually building HTML for a page, where you do roughly 100 string appends. 这是一个真实世界的例子,假设您正在手动为页面构建HTML,您可以在其中执行大约100个字符串追加。 If you did this with immutable strings, the JAVA virtual machine would do quite a bit of memory allocation and deallocation where with a StringBuffer it would do far less. 如果使用不可变字符串执行此操作,JAVA虚拟机将执行相当多的内存分配和释放,使用StringBuffer可以做得更少。

StringBuffer is a very popular choice with programmers. StringBuffer是程序员非常喜欢的选择。

It has the advantage over standard String objects, in that it is not an immutable object. 它具有优于标准String对象的优点,因为它不是不可变对象。 Therefore, if a value is appended to the StringBuffer, a new object is not created (as it would be with String), but simply appended to the end. 因此,如果将值附加到StringBuffer,则不会创建新对象(与String一样),而只是附加到末尾。

This gives StringBuffers (under certain situations that cannot be compensated by the compiler) a performance advantage. 这使StringBuffers(在某些情况下无法由编译器补偿)具有性能优势。

I tend to use StringBuffers anywhere that I dynamically add data to a string output, such as a log file writer, or other file generation. 我倾向于在任何可以动态地将数据添加到字符串输出的地方使用StringBuffers,例如日志文件编写器或其他文件生成。

The other alternative is StringBuilder. 另一种选择是StringBuilder。 However, this is not thread-safe, as was designed not to be to offer even better performance in single-threaded applications. 但是,这不是线程安全的,因为它不是为了在单线程应用程序中提供更好的性能。 Apart from method signatures containing the sychronized keyword in StringBuffer, the classes are almost identical. 除了在StringBuffer中包含sychronized关键字的方法签名之外,这些类几乎相同。

StringBuilder is recommended over StringBuffer in single threaded applications however, due to the performance gains (or if you look at it the other way around, due to the performance overheads of StringBuffer). 在单线程应用程序中,建议使用StringBuilder而不是StringBuffer,因为性能提升(或者如果你反过来看,由于StringBuffer的性能开销)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM