The path to the architect seventh Day ramble on using threadlocal to improve your level of division

Source: Internet
Author: User
Tags variables thread

One, what is threadlocal

Java.lang.threadlocal,threadlocal is provided as early as JDK 1.2 to provide a new way to solve the concurrency problems of multithreaded threads. Use this tool class to write elegant multithreaded programs very succinctly.

Threadlocal is easy to words too literally, assuming that it is a "local thread". In fact, threadlocal is not a thread, but a local variable of thread, perhaps naming it as threadlocalvariable is easier to understand.

When using threadlocal to maintain a variable, threadlocal provides a separate copy of the variable for each thread that uses the variable, so each thread can independently alter its own copy without affecting the counterpart of the other thread.

From a thread's point of view, the target variable is like a thread's local variable, which is what the "locals" in the class name means.

Thread-local variables are not a new Java invention, and many languages (such as IBM IBM XL FORTRAN) provide thread-local variables at the syntactic level. In Java, support is not provided at the language level, but in disguise through threadlocal classes.

As a result, code that writes thread-local variables in Java is relatively clumsy, making thread-local variables less popular among Java developers.

interface method of Threadlocal

Threadlocal class interface is very simple, there are only 4 methods, we first to understand:

2 void Set (Object value)

Sets the value of the thread local variable for the current thread.

2 Public Object get ()

This method returns the thread-local variables that correspond to the current thread.

2 Public void Remove ()

Deletes the value of the current thread local variable to reduce memory footprint, which is a new method for JDK 5.0. It is important to note that when the thread ends, the local variable that should be thread is automatically garbage collected, so it is not necessary to explicitly call the method to purge the thread's local variables, but it can speed up the memory collection.

2 protected objectinitialvalue ()

Returns the initial value of the thread's local variable, which is a protected method, apparently designed to allow subclasses to overwrite. This method is a deferred invocation method that executes only 1 times when the thread calls get () or set (Object) for the 1th time. The default implementation in Threadlocal returns a null directly.

It is worth mentioning that in JDK5.0, Threadlocal has supported generics, and the class name of the class has changed to threadlocal<t>. The API methods are also adjusted accordingly, and the new version of the API method is void set (t value), t get () and T InitialValue ().

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.