---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------
ThreadLocal:
实现线程范围内的数据共享,一个ThreadLocal代表一个变量,故其中只能存放一个数据,若有多个数据都要实现线程范围内的数据共享,该怎么处理呢?
此时就要将这多个变量封装到一个类中,然后将这个类存放到ThreadLocal中。
import java.util.Random;
public class ThreadLocalDemo {
private static int data;
private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
synchronized (ThreadLocalDemo.class) {
data = random.nextInt();
System.out.println(Thread.currentThread().getName()
+ " " + data);
tl.set(data);
}
System.out.println(Thread.currentThread().getName()
+ "A " + new ModuleA().getData());
System.out.println(Thread.currentThread().getName()
+ "B " + new ModuleB().getData());
}
}).start();
}
}
static class ModuleA {
public int getData() {
return tl.get();
}
}
static class ModuleB {
public int getData() {
return tl.get();
}
}
}
线程范围内的多个变量的共享:
Person类
public class 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;
}
}
ThreadLocalDemo 类
import java.util.Random;
public class ThreadLocalDemo {
private static int data;
private static ThreadLocal<Person> tl = new ThreadLocal<Person>();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
synchronized (ThreadLocalDemo.class) {
data = random.nextInt();
System.out.println(Thread.currentThread().getName()
+ " " + data);
Person person = new Person();
person.setAge(data);
person.setName(Thread.currentThread().getName() + data);
tl.set(person);
}
System.out.println(Thread.currentThread().getName()
+ "A " + new ModuleA().getData());
System.out.println(Thread.currentThread().getName()
+ "B " + new ModuleB().getData());
}
}).start();
}
}
static class ModuleA {
public String getData() {
return tl.get().getName() + tl.get().getAge();
}
}
static class ModuleB {
public String getData() {
return tl.get().getName() + tl.get().getAge();
}
}
}
但是这样做并不好,修改代码(这样做封装了ThreadLocal,使得外界看不到ThreadLocal):
Person类
public class Person {
static ThreadLocal<Person> tl = new ThreadLocal<Person>();
private static Person 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;
}
private Person() {
}
public synchronized static Person getThreadInstance() {
person = tl.get();
if (person == null) {
person = new Person();
tl.set(person);
}
return person;
}
}
ThreadLocalDemo 类
import java.util.Random;
public class ThreadLocalDemo {
private static int data;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
synchronized (ThreadLocalDemo.class) {
data = random.nextInt();
System.out.println(Thread.currentThread().getName()
+ " " + data);
Person person = Person.getThreadInstance();
person.setAge(data);
person.setName(Thread.currentThread().getName() + data);
Person.tl.set(person);
}
System.out.println(Thread.currentThread().getName()
+ "A " + new ModuleA().getData());
System.out.println(Thread.currentThread().getName()
+ "B " + new ModuleB().getData());
}
}).start();
}
}
static class ModuleA {
public String getData() {
return Person.tl.get().getName() + Person.tl.get().getAge();
}
}
static class ModuleB {
public String getData() {
return Person.tl.get().getName() + Person.tl.get().getAge();
}
}
}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------