您的位置:首页 > 职场人生

黑马程序员-线程范围内的数据共享之ThreadLocal

2014-04-07 14:05 337 查看
---------------------- <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>、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ThreadLocal