Differences between Java thread Local variables threadlocal and private variables

Source: Internet
Author: User

Threadlocal is not a thread, but a local variable of thread, and perhaps naming it as threadlocalvariable is easier to understand. Therefore, the code to write thread-local variables in Java is relatively clumsy, resulting in thread-local variables not being well-popularized in Java developers. Threadlocal interface method Threadlocal class interface is very simple, there are only 4 methods, let us first understand: void set (Object value) public void remove () Removes the value of the current thread local variable, The goal is to reduce the memory footprint. the principle of threadlocalThere is a map in the Threadlocal class that is used to A copy of the variable that stores each thread。 For example, the following sample implementation: public class threadlocalprivate Map values = Collections.synchronizedmap (new HashMap ());p ublic Object get () { Thread curthread = Thread.CurrentThread (), Object o = Values.get (Curthread), if (o = = null &&!values.containskey (c Urthread)) {o = InitialValue (); Values.put (Curthread, O);} Values.put (Thread.CurrentThread (), newvalue); return o;} Public Object InitialValue () {return null;}} Demo Code
 Public classThreadlocalexample { Public Static classMyrunnable implements Runnable {PrivateThreadlocal<integer> ThreadLocal =NewThreadlocal<integer>(); intLocal =0; @Override Public voidrun () {threadLocal.Set((int) (Math.random () *100D)); Local= (int) (Math.random () *100D); Try{Thread.Sleep ( -); } Catch(Interruptedexception e) {} System. out. println ("threadLocal:"+threadlocal.Get()); System. out. println ("Local:"+local); }    }     Public Static voidMain (string[] args) {myrunnable sharedrunnableinstance=Newmyrunnable (); Thread Thread1=NewThread (sharedrunnableinstance); Thread thread2=NewThread (sharedrunnableinstance);        Thread1.start ();        Thread2.start (); Try{thread1.join ();         Thread2.join (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //wait for the thread 1 to terminate//wait for the thread 2 to terminate    }}

Differences between Java thread Local variables threadlocal and private variables

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.