Threadlocal in Java

Source: Internet
Author: User

Threadlocal is typically used to hold multiple threads modifying shared variables so that each thread has access to its own modified variable value. My rough impression of threadlocal was that it was a map< thread that shared variable values, and it was not clear how it would be implemented. Encounter the concept of a relatively vague knowledge points, only to understand, to make progress, so the following analysis;

One application:
Declare a shared variable with threadlocal

instance variables in a normal class

1.1 Testing shared variables with threadlocal

Results:

1.2 Testing for common shared variables

Results:

Analysis:
In 1.1 and 1.2, the object that owns the threadlocal variable is passed multiple threads, and then the values in the object are modified. The changes made by each thread in 1.1 can be recorded, while in 1.2 each thread shares a variable in an object, and only one of the values is recorded for concurrency reasons. Threadlocal does record changes to shared variables for each thread.

Two source
Class diagram for threadlocal:

Each instance variable in the class:
Final int Threadlocalhashcode: The hashcode that uniquely identifies the Threadlocal object, which is used with the length-1 in the object array to obtain the position of the saved object.
Static Atomicinteger Nexthashcode: Used to generate a unique hashcode
static final int hash_increment: The increment of each renaissance into a hashcode

Methods in class: Set and get

Getmap (thread T): Gets the thread's threadlocal.threadlocalmap based on thread T threadlocals,threadlocalmap a bit like hash table, Used to store values corresponding to different threadlocal objects, a thread can store values stored through multiple different threadlocal operations.

Createmap (thread T, T firstvalue): Creates a threadlocalmap of thread T, and saves the Threadlocal object corresponding to the value firstvalue.

Setinitialvalue (): Creates the threadlocal.threadlocalmap of the thread, takes InitialValue () initialized value, creates threadlocalmap if the map is empty, and saves the initial value. If map is not empty, the value corresponding to the Threadlocal object in Threadlocalmap is replaced by the initial value, and the initial value is returned.

Three processes
Threadlocal the process of storing multiple thread operation values is as follows: Each thread will have a threadlocalmap created, and the map holds the values saved by different threadlocal. Gets the value corresponding to the Threadlocal object under the thread by manipulating Threadlocalmap. The Threadlocalmap object is saved with entry, which inherits the virtual reference WeakReference, and then the virtual reference.

Threadlocal in Java

Related Article

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.