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

[Java]View Plaincopy
  1. Import Java.util.HashMap;
  2. Import Java.util.Map;
  3. Import Java.util.Random;
  4. Public class Threadscopesharedata {
  5. private static int data = 0;
  6. private static Map<thread, integer> threaddata = new hashmap<thread,integer> ();
  7. public static void Main (string[] args) {
  8. For (int i = 0;i<2;i++) {
  9. New Thread (new Runnable () {
  10. public Void Run () {
  11. int data = new Random (). Nextint ();
  12. System.out.println (Thread.CurrentThread (). GetName () +
  13. "has put data:" +data);
  14. Threaddata.put (Thread.CurrentThread (), data);
  15. new A (). get ();
  16. new B (). get ();
  17. }
  18. }). Start ();
  19. }
  20. }
  21. static class a{
  22. public Void Get () {
  23. int data = Threaddata.get (Thread.CurrentThread ());
  24. System.out.println ("A from" +thread.currentthread (). GetName () +
  25. "Get Data:" +data);
  26. }
  27. }
  28. static class b{
  29. public Void Get () {
  30. int data = Threaddata.get (Thread.CurrentThread ());
  31. System.out.println ("B from" +thread.currentthread (). GetName () +
  32. "Get Data:" +data);
  33. }
  34. }
  35. }


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

Related Article

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.