public class StringInterner extends Object
StringInterner only guarantees it will behave in a correct manner. When you ask it for a String for a given input, it must return a String which matches the toString() of that CharSequence.
It doesn't guarantee that all threads see the same data, nor that multiple threads will return the same String object for the same string. It is designed to be a best-effort basis so it can be as lightweight as possible.
So while technically not thread safe, it doesn't prevent it operating correctly when used from multiple threads, but it is faster than added explicit locking or thread safety. NOTE: It does rely on String being thread safe, something which was guaranteed from Java 5.0 onwards c.f. JSR 133.
Discussion https://stackoverflow.com/questions/63383745/string-array-needless-synchronization/63383983
Modifier and Type | Field and Description |
---|---|
protected @NotNull String[] |
interner |
protected int |
mask |
protected int |
shift |
protected boolean |
toggle |
Constructor and Description |
---|
StringInterner(int capacity) |
Modifier and Type | Method and Description |
---|---|
@Nullable String |
intern(@Nullable CharSequence cs) |
protected boolean |
toggle() |
int |
valueCount() |
@NotNull protected final @NotNull String[] interner
protected final int mask
protected final int shift
protected boolean toggle
public StringInterner(int capacity) throws IllegalArgumentException
IllegalArgumentException
@Nullable public @Nullable String intern(@Nullable @Nullable CharSequence cs)
protected boolean toggle()
public int valueCount()
Copyright © 2021. All rights reserved.