Java--Inter-thread variable sharing

Source: Internet
Author: User
Import Java.util.Random;

public class Statictest {
	
	private static int data = 0;

	public static void Main (string[] args) {
		//Open three threads for
				(int i=0;i<3;i++) {
					new Thread (new Runnable () {

						@ Override public
						Void Run () {
							//each thread data value is the same, the program below has the explanation
						 data = new Random (). Nextint ();
							System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data);
						}
						
					}). Start ();}}


Run Result:

Thread-1 has put data:367396051
Thread-2 has put data:367396051
Thread-0 has put data:367396051

Import Java.util.HashMap;
Import Java.util.Map;


Import Java.util.Random;
	public class Threadscopesharedata {//static, which results in the same random value, a bit does not understand the private static int = 0;
	
	With thread as key private static map<thread,integer> Threaddata = new Hashmap<thread, integer> (); public static void Main (string[] args) {//Open three threads for (int i=0;i<3;i++) {new Thread (new Runnable () {@Overri De public void Run () {//each thread data value is the same, because it is a static variable, change all, the final result is the same as the last thread data value//is overwritten by the later thread data value, because the static variable has only one copy//dat
					A = new Random (). Nextint ();
					So it's different. 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);

 }
	}
}

The map simulates threadlocal.

Run Result:

Thread-0 has put data:1500573848
Thread-2 has put data:1307429900
Thread-1 has put data:-1173968999
A from Thread-1 get data:-1173968999
A from Thread-0 get data:1500573848
A from Thread-2 get data:1307429900
B from Thread-1 get data:-1173968999
B from Thread-0 get data:1500573848
B from Thread-2 get data:1307429900


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.