Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ThreadLocal

public class ThreadLocal
extends Object

java.lang.Object
   ↳ java.lang.ThreadLocal<T>
Known Direct Subclasses


Implements a thread-local storage, that is, a variable for which each thread has its own value. All threads share the same ThreadLocal object, but each sees a different value when accessing it, and changes made by one thread do not affect the other threads. The implementation supports null values.

See also:

Summary

Public constructors

ThreadLocal()

Creates a new thread-local variable.

Public methods

T get()

Returns the value of this variable for the current thread.

void remove()

Removes the entry for this variable in the current thread.

void set(T value)

Sets the value of this variable for the current thread.

Protected methods

T initialValue()

Provides the initial value of this variable for the current thread.

Inherited methods

From class java.lang.Object

Public constructors

ThreadLocal

Added in API level 1
ThreadLocal ()

Creates a new thread-local variable.

Public methods

get

Added in API level 1
T get ()

Returns the value of this variable for the current thread. If an entry doesn't yet exist for this variable on this thread, this method will create an entry, populating the value with the result of initialValue().

Returns
T the current value of the variable for the calling thread.

remove

Added in API level 1
void remove ()

Removes the entry for this variable in the current thread. If this call is followed by a get() before a set(T), #get() will call initialValue() and create a new entry with the resulting value.

set

Added in API level 1
void set (T value)

Sets the value of this variable for the current thread. If set to null, the value will be set to null and the underlying entry will still be present.

Parameters
value T: the new value of the variable for the caller thread.

Protected methods

initialValue

Added in API level 1
T initialValue ()

Provides the initial value of this variable for the current thread. The default implementation returns null.

Returns
T the initial value of the variable.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.