Data sharing in threadlocal threads (isolation between threads)

Source: Internet
Author: User
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 ()));}}}

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.