Threadlocal simulation and interpretation in Java

Source: Internet
Author: User

I. Test code

Public class xy_threaddata
{
Private Static Integer Data = 0;
Private Static Map <thread, integer> map = new hashmap <thread, integer> ();
Private Static threadlocal <integer> Local = new threadlocal <integer> ();

Public static void setdata (integer value)
{
Data = value;
}

Public static integer getdata ()
{
System. Out. println ("threadname:" + thread. currentthread () + "data value:" + data );
Return data;
}

Public static void setmapdata (integer value)
{
Map. Put (thread. currentthread (), value );
}

Public static integer getmapdata ()
{
Object OBJ = map. Get (thread. currentthread ());
System. Out. println ("threadname:" + thread. currentthread () + "MAP value:" + OBJ );
Return integer. parseint (obj. tostring ());
}

Public static void setthreadlocaldata (integer value)
{
Local. Set (value );
}

Public static integer getthreadlocaldata ()
{
Object OBJ = Local. Get ();
System. Out. println ("threadname:" + thread. currentthread () + "threadlocal value:" + OBJ );
Return integer. parseint (obj. tostring ());
}
}

Public class xy_threaddata_test
{
Public static void main (string [] ARGs)
{
For (INT I = 0; I <10; I ++)
{
New thread (New runnable (){
Public void run ()
{
Final int value = new random (). nextint (); // variable created by each thread
Xy_threaddata.setdata (value );
Xy_threaddata.getdata ();
}
}). Start ();
}

}
}
Threadname: thread [thread-3, 5, main] data value: 1046062244
Threadname: thread [thread-6, 5, main] data value:-879673875
Threadname: thread [thread-2, 5, main] data value:-125397465
Threadname: thread [thread-4, 5, main] data value:-1546413071
Threadname: thread [thread-0, 5, main] data value: 754770101
Threadname: thread [thread-8, 5, main] data value:-1666786926
Threadname: thread [thread-5, 5, main] data value:-1666786926
Threadname: thread [thread-9, 5, main] data value: 1046062244
Threadname: thread [thread-1, 5, main] data value: 269410746
Threadname: thread [thread-7, 5, main] data value: 269410746
Analysis: we can see that the data values in the following two threads are the same, and each thread variable is not unique.
Threadname: thread [thread-8, 5, main] data value:-1666786926
Threadname: thread [thread-5, 5, main] data value:-1666786926


Public class xy_threaddata_test
{
Public static void main (string [] ARGs)
{
For (INT I = 0; I <10; I ++)
{
New thread (New runnable (){
Public void run ()
{
Final int value = new random (). nextint ();
Xy_threaddata.setmapdata (value );
Xy_threaddata.getmapdata ();
}
}). Start ();
}

}
}
Threadname: threads [thread-0, 5, main] MAP value:-1138167111
Threadname: thread [thread-4, 5, main] MAP value:-1545929782
Threadname: thread [thread-6, 5, main] MAP value:-1612385717
Threadname: thread [thread-3, 5, main] MAP value:-1390594683
Threadname: thread [thread-8, 5, main] MAP value: 518506934
Threadname: thread [thread-2, 5, main] MAP value: 1583239372
Threadname: thread [thread-5, 5, main] MAP value: 995578601
Threadname: thread [thread-1, 5, main] MAP value:-916627474
Threadname: thread [thread-7, 5, main] MAP value:-960206804
Threadname: thread [thread-9, 5, main] MAP value:-1187504747
Analysis: The simulated thread variables are unique and have no duplicates.


Public class xy_threaddata_test
{
Public static void main (string [] ARGs)
{
For (INT I = 0; I <10; I ++)
{
New thread (New runnable (){
Public void run ()
{
Final int value = new random (). nextint ();
Xy_threaddata.setthreadlocaldata (value );
Xy_threaddata.getthreadlocaldata ();
}
}). Start ();
}

}
}
Threadname: thread [thread-1, 5, main] threadlocal value: 935024745
Threadname: thread [thread-4, 5, main] threadlocal value: 1207176846
Threadname: thread [thread-7, 5, main] threadlocal value:-1503260374.
Threadname: thread [thread-9, 5, main] threadlocal value:-1538563684.
Threadname: thread [thread-6, 5, main] threadlocal value: 955259906
Threadname: thread [thread-8, 5, main] threadlocal value: 894428541
Threadname: thread [thread-3, 5, main] threadlocal value: 730986356
Threadname: thread [thread-2, 5, main] threadlocal value:-540225655.
Threadname: thread [thread-5, 5, main] threadlocal value:-2003809947.
Threadname: thread [thread-0, 5, main] threadlocal value: 1917431015
Analysis: the thread variables are unique and have no duplicates.

 

Ii. threadlocal Analysis

Key Aspect 1
Threadlocal is not used to solve the problem of multi-thread access to shared objects. Generally, threadlocal is used. the objects from set () to the thread are the objects used by the thread. Other threads do not need to be accessed or cannot be accessed. Different threads access different objects.

 

Key Aspect 2
Threadlocal enables each thread to maintain an independent object, not through threadlocal. Set (),An object created through the operation of the new object in each thread. Each thread creates a copy or copy of an object.. Threadlocal. set () saves the reference of the newly created object to a map of each thread. Each thread has such a map and runs threadlocal. in get (), each thread extracts the objects from its own map, so it obtains the objects in its own thread, and the threadlocal instance is used as the map key.

 

Key Aspect 3
If threadlocal. set () is the same object shared by multiple threads, so threadlocal of multiple threads. get () still gets the shared object itself, but there are still concurrent access problems.


For more information about threadlocal, see: http://www.iteye.com/topic/103804

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.