Package multithreading; import Java. util. random; // threadlocal <integer> is implemented using Map <thread, integer> internally, independent data between threads // a single localdata variable // a collection of multiple threadscopedata variables public class threadlocaltest {static threadlocal <integer> localdata = new threadlocal <integer> (); static Class A {public static void getdata () {system. out. println ("Class A in" + thread. currentthread (). getname () + "Get localdata:" + localdata. get (); // obtain threadscoped data of the current thread Ata data = threadscopedata. getinstance (); system. out. println ("Class A in" + thread. currentthread (). getname () + "Get threadscopedata:" + data. name + data. age) ;}} static Class B {public static void getdata () {system. out. println ("Class B in" + thread. currentthread (). getname () + "Get localdata:" + localdata. get (); // obtain the data of the current thread threadscopedata DATA = threadscopedata. getinstance (); system. out. println ("Class B in" + thread. currentthread (). g Etname () + "Get threadscopedata:" + data. name + data. age) ;}} public static void main (string [] ARGs) {for (INT I = 1; I <= 2; I ++) {// start two threads new thread (New runnable () {@ overridepublic void run () {// generate and save data int intdata = new random (). nextint (100 ); // ================================= 1 variable ======================== ============== system. out. println (thread. currentthread (). getname () + "generate data" + intdata + ". Save in localdata "); localdata. set (intdata ); // ================================= multiple variables ==================== ============== system. out. println (thread. currentthread (). getname () + "produce data stored in threadscopedata"); threadscopedata DATA = threadscopedata. getinstance (); data. age = intdata; data. name = "name" + String. valueof (intdata ); // =============== data de-processing of the thread module ==============================. getdata (); B. getdata ();}}). start () ;}} class threadscopedata {public string name; Public int age; // Private threadscopedata () {} Private Static threadlocal <threadscopedata> map = new threadlocal <threadscopedata> (); public static threadscopedata getinstance () {threadscopedata instance = map. get (); // each thread has independent data if (instance = NULL) {instance = new threadscopedata (); map. set (Instance) ;}return instance ;}}
Principle
Package multithreading; import Java. util. hashmap; import Java. util. map; import Java. util. random; public class threadincludata {/*** multiple threads call the same code but independent data between threads * tread1 corresponds to connection1 (Conn. begintransaction... Remittance, withdrawal... Conn. Commit) * tread2 corresponds to connection2 (conn. begintransaction... Remittance, withdrawal... Conn. commit) * thread 1 should not have the same connection as thread 2. Otherwise, thread 2 will be submitted by thread 1 before it has been withdrawn. **/static Map <thread, integer> datamap = new hashmap <thread, integer> (); public static void main (string [] ARGs) {for (INT I = 0; I <3; I ++) {New thread (New runnable () {@ overridepublic void run () {int DATA = new random (). nextint (); datamap. put (thread. currentthread (), data); New option1 (). get (); New option2 (). put ();}}). start () ;}} static class o Ption1 {void get () {system. Out. println ("option1 thread:" + thread. currentthread (). getname () + ". Get data: "+ datamap. get (thread. currentthread () ;}} static class option2 {void put () {system. out. println ("option2 thread:" + thread. currentthread (). getname () + ". Put data: "+ datamap. Get (thread. currentthread ()));}}}