您的位置:首页 > 编程语言

多线程编程入门(8):线程范围内的共享变量之ThreadLocal

2016-07-31 00:06 417 查看
package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
//ThreadLocal方式实现线程范围内的共享变量
public class ThreadLocalTest {

private static Map<Thread,Integer> threadData = new HashMap<Thread,Integer>();//线程内的数据
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();//ThreadLocal方式实现线程内的数据共享(Integer)
private static  ThreadLocal<Person>  threadLocalPerson = new ThreadLocal<Person>();//ThreadLocal方式实现线程内的数据共享(Person对象)

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);//线程内的数据
threadLocal.set(data);//ThreadLocal方式实现线程内的数据共享
//Person person = new Person();
//person.setName("zhangsan_"+Thread.currentThread());
//person.setAge(data);
//threadLocalPerson.set(person);
Person.getThreadLocalInstance().setName("name"+data);
Person.getThreadLocalInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}

static class A{
public void get(){
//System.out.println("A from " + Thread.currentThread().getName() + " get data " +threadData.get(Thread.currentThread()));//线程内的数据
//System.out.println("A from " + Thread.currentThread().getName() + " get data " + threadLocal.get());//ThreadLocal方式实现线程内的数据共享
//System.out.println("A from " + Thread.currentThread().getName() + " get data " + threadLocalPerson.get());//ThreadLocal方式实现线程内的数据共享
System.out.println("A from " + Thread.currentThread().getName() + " get data " + Person.getThreadLocalInstance());
}
}

static class B{
public void get(){
//System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadData.get(Thread.currentThread()));//线程内的数据
//System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadLocal.get());//线程内的数据
//System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadLocalPerson.get());//ThreadLocal方式实现线程内的数据共享
System.out.println("A from " + Thread.currentThread().getName() + " get data " + Person.getThreadLocalInstance());
}
}
}

class Person{

private Person(){}

//与线程有关的单例
public static /*synchronized*/ Person getThreadLocalInstance(){
Person person = threadLocal.get();//与本线程有关的Person对象
if(person == null){
person = new Person();
threadLocal.set(person); //与本线程有关的Person对象
}
return person;//与本线程有关的Person对象
}

//private static Person  person = null; //new Person();
private static ThreadLocal<Person> threadLocal = new ThreadLocal<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;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}

参考链接

https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: