Drp-threadlocal a simple understanding

Source: Internet
Author: User

Simple is a jar package. In a simple thing is a tool class! What can the tool do? The tool is used to write multithreaded programs, line. Multithreading is effective. You can only go to the Internet to find information, because today we just 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 corresponding 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 corresponding thread are actively 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.
Method is a deferred call method that runs only 1 times when the thread calls get () or set (Object) for the 1th time.

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. It's obviously designed to make the subclass cover.

The above methods we are able to query the API documentation. By now the threadlocal has become more powerful, adding to the concept of generics. void Set (t value), t get (), and T InitialValue (), we are able to manipulate multiple data types.

In the synchronization mechanism, the lock mechanism of the object ensures that only one thread is interviewed at the same time. At this point the variable is shared by multiple threads, and using a synchronization mechanism requires the program to carefully analyze when the variable is read and written. When you need to lock an object, when to release the object lock and other complex problems. Programming and writing are relatively difficult.
Threadlocal, in addition, has an angle to solve the multi-threaded concurrency interview. When writing multithreaded code, it is possible to encapsulate unsafe variables into threadlocal.
Because the threadlocal can hold whatever type of object, the Get () provided by the lower version JDK returns the object. You need to force type conversions.

But JDK 5.0 through generics very good to overcome this problem, to some extent simplifying the use of Threadlocal, code Listing 9 2 uses the JDK 5.0 new threadlocal<t> version number.
In summary, for multi-threaded resource sharing problem, the synchronization mechanism uses the "time to change space" way. Previously wrote a blog post: "The application of synchronized" and threadlocal adopted the "space for Time" way.

The former provides only one copy of the variable, allowing different threads to queue up for access, and the latter provides a variable for each thread so that it can be visited at the same time without affecting each other.

About threadlocal This part is not deep understanding, what is wrong, please correct the timely reader.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Drp-threadlocal a simple understanding

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.