Drp-threadlocal Simple Comprehension

Source: Internet
Author: User

in short, it is a class of JAR package encapsulation, in a simple point is a tool class! What is this tool class capable of? This tool class is used to write multi-threaded program, OK, multithreading is what, we can only go online to find information, because today we only to introduce threadlocal knowledge.

Let's take a look at the interface method of the Threadlocal package:

void set (Object value) sets the value of the thread local variable for the current thread.
Public Object Get () This method returns the thread local variable that corresponds to the current thread.
The public void Remove () Removes the value of the current thread local variable to reduce memory usage, which is a new method of JDK 5.0. It is important to note that when the thread ends, the local variables of the thread that should be used are automatically garbage collected, so it is not necessary to explicitly call the method to clear the thread's local variables, but it can speed up the memory reclamation.
The method is a deferred call method that executes only when the thread calls get () or set (Object) for the 1th time, and executes only 1 times. The default implementation in Threadlocal returns a null directly. Protected Object InitialValue () returns the initial value of the thread's local variable, which is a protected method, apparently designed to allow subclasses to overwrite.

The above methods we can query the API document, and now the threadlocal has become more powerful, adding the concept of generics, void set (t value), t get () and T InitialValue (), we can work on a variety of data types.

In the synchronization mechanism, the lock mechanism of the object guarantees that only one thread accesses the variable at the same time. At this time the variable is shared by multiple threads, using the synchronization mechanism requires the program to carefully analyze when to read and write variables, when to lock an object, when to release object locks and other complex problems, programming and writing is relatively difficult.
Threadlocal, however, solves multiple threads of concurrent access from another angle. When writing multithreaded code, you can encapsulate unsafe variables into threadlocal.
Because the threadlocal can hold any type of object, the Get () provided by the lower version of the JDK returns an object, which requires a type cast. But JDK 5.0 solves this problem with generics, and simplifies the use of threadlocal to some extent, and listing 9 2 uses the new threadlocal<t> version of JDK 5.0.
To sum up, for multi-threaded resource sharing problem, the synchronization mechanism adopted the "time for Space" way, once wrote a blog post: "Synchronized Application" and threadlocal adopted the "space for Time" way. The former provides only one copy of the variable, allowing different threads to queue access, and the latter provides a variable for each thread, so it can be accessed at the same time without affecting each other.

About threadlocal This part of the understanding is not too deep, there are any mistakes please the reader to correct.

Drp-threadlocal Simple Comprehension

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.