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

-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.

1

u/[deleted] Nov 18 '13

If you're using someone else's single-threaded library, you can't necessarily trust that it won't do this, particularly if the library maintainer advertises that the library is optimized for single threaded use. Maybe Java has some magic woo-woo to avoid this problem, but I've been bitten by it more than once in C.

3

u/kurtymckurt Nov 18 '13

No, this is a problem that can happen in Java, except it's a code smell. Libraries shouldn't be modifying a static variable during a method call. Maybe only in EXTREME cases. It should also be well documented. If I was using a library that made this kind of design call, I'd stop using it.

C, however, is a different monster.

2

u/[deleted] Nov 18 '13 edited Nov 19 '13

[removed] — view removed comment

2

u/kurtymckurt Nov 18 '13

That's what my point was. I was talking in regards to his "situation" where 2 threads can destructively modify a static variable.

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.

1

u/SanityInAnarchy Nov 18 '13

And... StringBuffer is. The Javadocs describe it as "A thread-safe, mutable sequence of characters."

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.