The following is the threadlocal test code. For more information, see note
Package COM. jadyer. thread. local; import Java. util. random; /*** threadlocal test * @ see ================================== ========================================================== ================================================================= *@ the role and purpose of see threadlocal * @ see threadlocal is used to achieve data sharing within the thread. That is, for the same code, multiple modules share different data when running in different threads * @ see each thread calls global threadlocal. the Set () method is equivalent to adding a record to its internal map, the key is the respective thread, and the value is the respective set () the passed value * @ see ============================================== ========================================================== =============================================================== * @ see threadlocal application scenarios * @ See, for example, actioncontext in struts2, when a code is called and run by different threads, the data of this Code operation is the respective status and data of each thread * @ see for different threads, actioncontext. the objects obtained by the getcontext () method are different * @ see for the same thread Actioncontext. the getcontext () method obtains the same object no matter which module it is in or how many times it is called * @ see view com. opensymphony. xwork2.actioncontex's 43rd and 166 source code lines are not hard to find, struts2 does this * @ see ================================== ========================================================== =================================================================* @ see member variables and local variables * @ see member variables in the thread: when multiple threads operate on the member variables of the same object, their changes to the member variables affect each other * @ see local variables: Each thread will have a copy of this local variable, the changes made to local variables by one thread do not affect the operations performed by other threads on this local variable * @ see ======== ========================================================== ========================================================== ===============================* @ Author macro Yu * @ create Feb 27,201 2 12:10:24 am */public class threadlocaltest {public static void main (string [] ARGs) {New thread (New mythread (new random (). nextint ())). start (); New thread (New mythread (new random (). nextint ())). start () ;}} class mythread implements runnable {private integer data; Public Mythread (integer data) {This. data = data ;}@ overridepublic void run () {system. out. println (thread. currentthread (). getname () + "has put data:" + data); User. getthreadinstance (). setname ("name" + data); User. getthreadinstance (). setage (data); New Pig (). getmydata (); new dog (). getmydata () ;}} class pig {public void getmydata () {user = user. getthreadinstance (); system. out. println ("pig from" + thread. currentth Read (). getname () + "getmydata:" + User. getname () + "|" + User. getage () ;}} class dog {public void getmydata () {user = user. getthreadinstance (); system. out. println ("Dog from" + thread. currentthread (). getname () + "getmydata:" + User. getname () + "|" + User. getage () ;}}/*** shared objects within the custom thread range. That is, this class will create an independent object for different threads * @ see. At this time, each thread will get its own instance, there is no association between the instances obtained by each thread * @ see we can use it to compare it with the lazy style in singleton mode, this is very interesting. * @ see struts2 is designed in this way, each of its requests is a thread */class user {Private Static threadlocal <user> instancemap = new threadlocal <user> (); private user () {}/*** get the instance related to the current thread */public static/* synchronized */user getthreadinstance () {user instance = instancemap. get (); If (null = instance) {instance = new user (); instancemap. set (Instance) ;}return instance ;}private string name; private int age; Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age ;}}