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>(); public static void main(String[] args) {for (int i = 0; i < 2; i++) {new Thread(new Runnable() {@Overridepublic 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);}}}
The thread shares the variable data, sets the data as a static variable, places it in the map when the thread is assigned a random value, and obtains it when the new A or B class is used.
When assigning values to data, you need to redefine the new memory block. If an int variable is not added, the output values are exactly the same.