Android multithreaded Research (6)--data isolation between multiple threads

Source: Internet
Author: User

In the previous article, "Android Multithreading (5)--sharing data between threads," the data sharing between threads has been studied and researched, and this article looks at how to solve the problem of data isolation between multiple threads and what is data isolation? For example, we now open two threads, all two threads to the same global variable data assignment, each thread operation of its assigned variables, this requires isolation. Look at the code first:

Import Java.util.random;public class Threadlocaltest {private static int data = 0;public static void Main (string[] args) { for (int i=0; i<2; i++) {new Thread (new Runnable () {@Overridepublic void run () {data = new Random (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); new A (). get (); new B (). get ();}}). Start ();}} Static class A{public int get () {System.out.println ("A from" + Thread.CurrentThread (). GetName () + "have get data:" + data) ; return data;}} Static class B{public int get () {System.out.println ("B from" + Thread.CurrentThread (). GetName () + "have get data:" + data) ; return data;}}}
Operation Result:

From the above we can see that both Thread-0 and Thread-1 are manipulating the variable data, but there is no isolation between the two threads, so two threads in the output share one data variable.

We will modify the above code as follows:

Import Java.util.hashmap;import Java.util.map;import Java.util.random;public class Threadlocaltest {//private static int data = 0;private static Map<thread, integer> Map = new Hashmap<thread, integer> ();p ublic static void Main ( String[] args) {for (int i=0; i<2; i++) {new Thread (new Runnable () {@Overridepublic void run () {//data = new Random (). NEX TInt (); int data = new Random (). Nextint (); Map.put (Thread.CurrentThread (), data); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); new A (). get (); new B (). get ();}}). Start ();}} Static class A{public int get () {System.out.println ("A from" + Thread.CurrentThread (). GetName () + "have get data:" + MAP.G ET (Thread.CurrentThread ())); return Map.get (Thread.CurrentThread ());}} Static class B{public int get () {System.out.println ("B from" + Thread.CurrentThread (). GetName () + "have get data:" + MAP.G ET (Thread.CurrentThread ())); return Map.get (Thread.CurrentThread ());}}}
Output Result:

In the above code, we use a map collection to isolate the thread's operation to the data, which is equivalent to creating a backup of the data (two copies of data) to enable the separation of the threads between the thread, in fact, as early as the Java 1.2 introduced a class to support thread data isolation (java.lang.ThreadLocal), let's look at how to use ThreadLocal to implement data isolation between threads.

Three methods in Threadlocal: '

Get (): Returns a copy of the thread's local variables for the current thread

Protected InitialValue (): Returns the initial value of the current thread of the thread's local variable

void set (Object value): Sets the value of the thread local variable copy of the current thread

Where the InitialValue method is a method written for a subclass that executes when a thread invokes the get () or set () method for the first time and executes only once.

Import Java.util.random;public class Threadlocaltest {//private static int data = 0;//private static Map<thread, intege r> map = new Hashmap<thread, integer> ();p rivate static threadlocal<integer> tl = new Threadlocal<intege R> ();p ublic static void Main (string[] args) {for (int i=0; i<2; i++) {new Thread (new Runnable () {@Overridepublic void Run () {//data = new random (). Nextint (); int data = new Random (). Nextint ();//map.put (Thread.CurrentThread (), data); Tl.set (data); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); new A (). get (); new B (). get ();}}). Start ();}} Static class A{public int get () {System.out.println ("A from" + Thread.CurrentThread (). GetName () + "have get data:" + tl.ge T ()); return Tl.get ();}} Static class B{public int get () {System.out.println ("B from" + Thread.CurrentThread (). GetName () + "have get data:" + tl.ge T ()); return Tl.get ();}}}
Operation Result:

The above code is significantly less, in fact threadlocal in the bottom of the map to store the variable copy implementation.




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.