Shared data in the Java thread scope

Source: Internet
Author: User

thread-wide shared data, more commonly used in Java EE, less Android

A thread calls A,b,c three modules, and the expression or variable call in the module accesses a data that can be static
Another thread also calls A,b,c three modules, and the data accessed by the expression or variable in the module is not just the data, but another

The same code, a piece of data on a thread

Import Java.util.hashmap;import Java.util.map;import Java.util.random;public class Threadscopesharedata {private static int data = 0;private static Map<thread, integer> threaddata = new hashmap<thread,integer> ();p ublic Stat IC void Main (string[] args) {for (int i = 0;i<2;i++) {New Thread (new Runnable () {public void run () {int data = new Random (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" +data); Threaddata.put (Thread.CurrentThread (), data); new A (). get (); new B (). get ();}}). Start ();}} Static class A{public void Get () {int data = Threaddata.get (Thread.CurrentThread ()); System.out.println ("A from" +thread.currentthread (). GetName () + "Get Data:" +data);}} Static class B{public void Get () {int data = Threaddata.get (Thread.CurrentThread ()); System.out.println ("B from" +thread.currentthread (). GetName () + "Get Data:" +data);}}}

Application scenario: A database, one method is to input data, one method is output data, two independent objects,


A in the input data to B, b in the outward output of the data,a->b->


C in the input data to D, d in the outward output of the data,c->d->


The two lines share the same linked object, if the C line is finished, and the result is submitted, then the thread where a is located is interrupted, so each thread is working on its own data, the thread


The data of the operation, which is the data within the scope of its own thread, regardless of the data on the other thread (bank transfer is the same)


The above examples demonstrate that the thread scope, (different modules) data sharing, the thread range outside, the data independent





Shared data in the Java thread scope

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.