public final class TemporaryThreadLocals extends Object
byte[] and StringBuilder. Keep in mind that the variables
provided by this class must be used with extreme care, because otherwise it will result in unpredictable
behavior.
Most common mistake is to call or recurse info a method that uses the same thread-local variable. For example, the following code will produce a garbled string:
> class A {
> @Override
> public String toString() {
> return TemporaryThreadLocals.get().append('"').append(new B()).append('"').toString();
> }
> }
> class B {
> @Override
> public String toString() {
> return TemporaryThreadLocals.get().append("foo").toString();
> }
> }
> // The following assertion fails, because A.toString() returns "foofoo\"".
> assert "\"foo\"".equals(new A().toString());
A rule of thumb is to use the thread-local variables provided by this class in a narrow scope where there's no chance of recursion or reentrance.
| Modifier and Type | Method and Description |
|---|---|
byte[] |
byteArray(int minCapacity)
Returns a thread-local byte array whose length is equal to or greater than the specified
minCapacity. |
char[] |
charArray(int minCapacity)
Returns a thread-local character array whose length is equal to or greater than the specified
minCapacity. |
static TemporaryThreadLocals |
get()
Returns the current
Thread's TemporaryThreadLocals. |
StringBuilder |
stringBuilder()
Returns a thread-local
StringBuilder. |
public static TemporaryThreadLocals get()
Thread's TemporaryThreadLocals.public byte[] byteArray(int minCapacity)
minCapacity.public char[] charArray(int minCapacity)
minCapacity.public StringBuilder stringBuilder()
StringBuilder.Copyright © 2020 LeanCloud. All rights reserved.