Implementation of the Java.lang.ThreadLocal class, usage

Source: Internet
Author: User
The Threadlocal class provides a place for each thread to store local variables.

Essentially, each currently running thread holds a map,threadlocal class that encapsulates access to the map, so that a new generated object can be placed into the map via threadlocal in a thread. This ensures that the object that the thread obtains at a later time from the Threadlocal object, the map, is only available in that thread and will not be accessed by other threads.

The article "Threadlocal Design and use" mentioned that threadlocal use similar to the following implementation is actually wrong;
public class ThreadLocal
{
Private MAP values = Collections.synchronizedmap (new HashMap ());
Public Object Get ()
{
Thread curthread = Thread.CurrentThread ();
Object o = values.get (curthread);
if (o = = null &&!values.containskey (curthread))
{
o = InitialValue ();
Values.put (Curthread, O);
}
return o;
}

public void Set (Object newvalue)
{
Values.put (Thread.CurrentThread (), newvalue);
}

Public Object InitialValue ()
{
return null;
}
}

This map is not actually kept by threadlocal, but each thread. The advantage of this is that every time the thread dies, all objects referenced in the map are collected by the garbage collector along with the death of this thread (assuming, of course, that it is not referenced elsewhere)
The map key is a weak reference to the Threadlocal object, and when the Threadlocal object is discarded, the garbage collector ignores the key reference and cleans out the Threadlocal object.


As recommended by Java doc, threadlocal is generally declared public static.

Method Summary
T get ()
Returns the value in the current thread copy of this thread local variable.
Protected T InitialValue ()
Returns the "initial value" of the current thread for this thread local variable.
void Remove ()
Removes the value of the current thread for this thread local variable.
void set (T value)
Sets the value in the current thread copy of this thread local variable to the specified value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.