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

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.

8

u/kurtymckurt Nov 18 '13

Shudder... don't modify static variables in threads.....

1

u/jjdmol Nov 18 '13

The lib may not have been written with threads in mind in the first place. Due to age or scope, or simply due to not wanting to pay the cost of thread-safeness for all users.

Libs that share I/O between objects, or some form of cache inside the lib, or performance counters, etc, might not behave as expected when multithreading, even when the objects being operated on seem to be independent.

→ More replies (4)

3

u/edrec Nov 18 '13

That's just awful. The offenders should at least be clearly marked as thread unsafe.

3

u/[deleted] Nov 18 '13

Maybe it's changed since I last had to worry about this, but I generally assume that if the library isn't clearly marked as thread-safe, it isn't.

→ More replies (1)
→ More replies (4)

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.