[Java]
Package cn. itcast. demo;
Import java. util. HashMap;
Import java. util. Map;
Import java. util. Random;
Public class ThreadScopeShareData {
/*
* Shared variables within the thread range
* Function: Shared variables within the thread scope refer to the write and read operations on the same variable at the same time by several threads,
* The data read by the same thread is the data written by itself.
*/
Private static int data = 0;
Private static Map <Thread, Integer> map = new HashMap <Thread, Integer> ();
Public static void main (String [] args ){
For (int I = 0; I <2; I ++ ){
New Thread (new Runnable (){
Public void run (){
Data = new Random (). nextInt ();
Map. put (Thread. currentThread (), data );
System. out. println (Thread. currentThread (). getName () + ": MAIN --->" + map. get (Thread. currentThread ()));
New A (). get ();
New B (). get ();
}
}). Start ();
}
}
Static class {
Public void get ()
{
System. out. println (Thread. currentThread (). getName () + "--->" + map. get (Thread. currentThread ()));
}
}
Static class B {
Public void get ()
{
System. out. println (Thread. currentThread (). getName () + "--->" + map. get (Thread. currentThread ()));
}
}
}