Threadlocal Class and usage tips

Source: Internet
Author: User

Java threading is a very basic and important technology, in the low-level and biased Java programs are inevitably used in Java multithreading technology, then the sharing of data is one of the problems we must consider, naturally I will think of threadlocal and synchronized.

Threadlocal mainly solves the problem that the data in multi-threading is inconsistent with concurrency. Threadlocal provides a copy of the data that is accessed concurrently in each thread and runs the business through the access replica, which consumes some memory, but can significantly reduce the performance cost of thread synchronization, while also reducing the complexity of thread concurrency control. Synchronized is the mechanism by which a lock is used so that a variable or block of code can be accessed by only one thread at a time. Although both are to solve the problem of multi-threaded concurrent access, but the difference is very large. Synchronized is used for data sharing between threads, while threadlocal is used for data isolation between threads. Here is a small example, when using threadlocal we can refer to its wording:

Import Java.util.hashmap;import Java.util.map;import Java.util.random;public class Threadlocaltest {private static threadlocal<integer> x = new threadlocal<integer> ();p rivate static threadlocal<mythreadscopedata> Mythreadscopedata = new threadlocal<mythreadscopedata> ();p ublic static void Main (string[] args) {for (int i=0;i <2;i++) {New Thread (new Runnable () {@Overridepublic void run () {int data = new Random (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); X.set (data); Mythreadscopedata.getthreadinstance (). SetName ("name" + data); Mythreadscopedata.getthreadinstance (). setage (data); new A (). get (); new B (). get ();}}). Start ();}} Static class A{public void Get () {int data = X.get (); System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Get data:" + data); Mythreadscopedata myData = Mythreadscopedata.getthreadinstance (); System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Getmydata:" + mydata.getname () + ", "+mydata.getage ());}} Static class B{public void Get () {int data = X.get (); System.out.println ("B from" + Thread.CurrentThread (). GetName () + "Get data:" + data); Mythreadscopedata myData = Mythreadscopedata.getthreadinstance (); System.out.println ("B from" + Thread.CurrentThread (). GetName () + "Getmydata:" + mydata.getname () + "," +mydata.getage () );}}} Class Mythreadscopedata{private static threadlocal<mythreadscopedata> map = new threadlocal< Mythreadscopedata> ();p rivate String name;private int age;private mythreadscopedata () {}public static/*synchronized */Mythreadscopedata Getthreadinstance () {Mythreadscopedata instance = Map.get (); if (instance = = null) {instance = new Mythreadscopedata (); Map.set (instance);} return instance;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Summarize:synchronized is the mechanism of using locksenables a variable or block of code to be accessed by only one thread at a time. Threadlocal, however, provides a copy of the variable for each thread, so that each thread accesses the same object at a certain time, isolating the data shared by multiple threads. Synchronized, by contrast, is used to gain data sharing when communicating between multiple threads. The specific use of what we want to see when programming the application environment depends.


Threadlocal Class and usage tips

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.