package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.Random; public class ThreadScopeShareData { 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(){ @Override 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); } } }
public static void main(String[] args) { new ThreadLocalTest().init(); } //init private void init(){ for(int i =0;i<2;i++){ new Thread(new Runnable() { public void run() { int data = new Random().nextInt(); Person.getThreadInstance().setName(Thread.currentThread().getName()); Person.getThreadInstance().setAge(data); new A().get(); new B().get(); } }).start(); } } //A class A { Person person = Person.getThreadInstance(); public void get(){ System.out.println("A:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge()); } } //B class B { Person person = Person.getThreadInstance(); public void get(){ System.out.println("B:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge()); } } //Person 将跟线程相关的绑定,放在共享的数据类的内部实现 static class Person{ private static ThreadLocal<Person> threadLocal = new ThreadLocal<ThreadLocalTest.Person>(); private Person(){ } public static Person getThreadInstance(){ Person person = threadLocal.get(); if(person==null){ person = new Person(); threadLocal.set(person); } return person;. } private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
public static final ThreadLocal<Person> threadlocal = new ThreadLocal(){
@Override protected Object initialValue() { return new Person(); } }; public static void main(String[] args) { new ThreadLocalTest().init(); } private void init(){ for(int i =0;i<2;i++){ new Thread(new Runnable() { public void run() { int data = new Random().nextInt(); threadlocal.get().setName(Thread.currentThread().getName()); threadlocal.get().setAge(data); new A().get(); new B().get(); } }).start(); } } //A class A { Person person = threadlocal.get(); public void get(){ System.out.println("A:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge()); } } //B class B { Person person = threadlocal.get(); public void get(){ System.out.println("B:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge()); } } //Person static class Person{ public Person(){ } private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }