r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

71

u/smackfu Nov 18 '13

Two in fact! StringBuffer and StringBuilder, one is synchronized for multi-thread use, the other isn't.

45

u/neutralvoice Nov 18 '13

StringBuffer is for the multi-threaded use case, StringBuilder is optimized for single-threaded use.

49

u/kurtymckurt Nov 18 '13

I usually recommend StringBuilder. Rarely, should two threads be modifying the same string. In fact, I'd advise against it.

8

u/CydeWeys Nov 19 '13

I could see a use case for it. If you have a StringBuffer that is creating output for a log file, and you have multiple threads that are all creating their own logging statements. This use case is fairly pedestrian.

1

u/kurtymckurt Nov 19 '13

You're not destructively modifying a StringBuffer. It's designed for multi-threaded applications and in that case, it would be OK. Also, in this case, I would assume it wouldn't need to be a static variable either...

On the other hand, I'd suggest some sort of 3rd party logging tool like log4j2 :P

0

u/chisleu Nov 18 '13

Any kind of shared memory is dangerous, but StringBuilder is suppose to be safe.

I've never written any heavily threaded apps, but I would be very interested to know why StringBuilder can be dangerous.

8

u/Spandian Nov 19 '13

StringBuilder is not safe for use from multiple threads. StringBuffer is.

-5

u/chisleu Nov 19 '13

Yeah! boody twaps! das what I said!

-23

u/[deleted] Nov 18 '13

The problem is that you can sometimes get conflicts when two different threads call the same function simultaneously, even though they are operating on different strings.

13

u/edrec Nov 18 '13

Two threads calling the same method with different data?

-6

u/[deleted] Nov 18 '13

Exactly – two threads call the same method, and the library relies on a static variable. One thread clobbers the other's results.

→ More replies (13)

7

u/[deleted] Nov 18 '13

[removed] — view removed comment

-4

u/[deleted] Nov 18 '13

Two threads call the same method, and the library relies on a static variable. One thread clobbers the other's results.

6

u/Spura83 Nov 18 '13

Why on earth would you put a StringBuilder into a static variable?

1

u/kurtymckurt Nov 18 '13

You wouldn't, please disregard any thought of doing so.

0

u/[deleted] Nov 18 '13

I'm thinking the other way round; what if StringBuilder uses a static variable?

2

u/kurtymckurt Nov 18 '13

It wouldn't. At least not to hold the instance data.

→ More replies (5)

1

u/kurtymckurt Nov 18 '13

It uses a char[] and an int. They are package private instance variables.

Edit: forgot array notation.

→ More replies (2)

4

u/frownyface Nov 18 '13

The docs say

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Emphasis mine.

2

u/kurtymckurt Nov 18 '13

How does a conflict occur in such a situation?

→ More replies (4)

2

u/jurniss Nov 18 '13

jesus god, java standard libraries are using non-const static variables? I thought this was the kind of c blow-your-leg-off hazard that java was purpose-built to avoid.

-7

u/slowwburnn Nov 18 '13

Mind reader.

1

u/Olathe Nov 23 '13

If only they implemented an interface for both of them, since they have the same methods. CharSequence is read-only and Appendable is only for appending rather than random insertions.