Part 2 thread Security (2) -- threadlocal variable

Source: Internet
Author: User

As mentioned above, the variable security issue.

1. Try not to let the thread run on the same object instance. This is a relatively low-efficiency method. In some cases, sharing objects between threads is inevitable.

2. encapsulate variables that may be shared in a method to make it an internal variable of the method. In this way, every thread has its own private variable value even if multiple threads concurrently execute this method.

The second method is relatively recommended.

The threadlocal variable is introduced below: A threadlocal variable can only store one proprietary object, if the thread needs multiple proprietary objects. Multiple threadlocal variables need to be designed.

The following code is used to understand threadlocal [threadlocaltest]

/** <Br/> * threadlocaltest. java <br/> * copyright (c) 2011 cuiran2001@163.com <br/> * Create: cui ran 2011-1-13 04:11:56 <br/> */</P> <p> package COM. cayden. thread812; </P> <p>/** <br/> * @ author Cui ran <br/> * @ version 1.0.0 <br/> * @ DESC <br/> */ <br/> public class threadlocaltest {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> runnable accumelatora = new accumulatort (); </P> <p> thread threada = new thread (accumelatora, "threada"); <br/> thread threadb = new thread (accumelatora, "threadb"); </P> <p> threada. start (); <br/> threadb. start (); <br/>}</P> <p >}< br/> class accumulatort implements runnable <br/>{</P> <p> threadlocal = new threadlocal (); <br/> @ override <br/> Public void run () {<br/> // todo auto-generated method stub </P> <p> for (INT I = 1; I <= 10; I ++) {<br/> If (threadlocal. get () = NULL) </P> <p> threadlocal. set (New INTEGER (0); </P> <p> int x = (integer) threadlocal. get ()). intvalue (); </P> <p> X = x + I; <br/> threadlocal. set (New INTEGER (x); <br/> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/> // todo: handle exception <br/>}< br/> system. out. println (thread. currentthread (). getname () + "/T accumulate and =" + (integer) threadlocal. get ()). intvalue (); <br/>}</P> <p>}

Running result:

Threada accumulate and = 1 <br/> threadb accumulate and = 1 <br/> threadb accumulate and = 3 <br/> threada accumulate and = 3 <br/> threada accumulate and = 6 <br/> threadb accumulation and = 6 <br/> threadb accumulation and = 10 <br/> threada accumulation and = 10 <br/> threada accumulation and = 15 <br/> threadb accumulation and = 15 <br/> threadb accumulation and = 21 <br/> threada accumulation and = 21 <br/> threada accumulation and = 28 <br/> threadb accumulation and = 28 <br/> threadb accumulation and = 36 <br/> threada accumulation and = 36 <br/> threada accumulation and = 45 <br/> threadb accumulation and = 45 <br /> threada accumulate and = 55 <br/> threadb accumulate and = 55 <br/>

Threadlocal is an advanced technique of multi-threaded programming and the ultimate solution to variable security issues. Storing variables that may have shared conflicts in threadlocal variables can effectively prevent tampering by other threads running the same code. Threadlocal establishes a one-to-one binding relationship between variables and threads.

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.