黑马程序员-线程范围内的数据共享之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中。
线程范围内的多个变量的共享:
Person类
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类
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>、期待与您交流! ----------------------
---------------------
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实现线程范围内的数据共享
- java-线程-用ThreadLocal类实现线程范围内的数据共享
- 黑马程序员--javaSE--ThreadLocal实现线程范围的共享变量
- ThreadLocal-------线程范围内的数据共享
- ThreadLocal实现线程范围内模块之间共享数据及线程间数据独立
- java多线程:5、线程范围内的数据共享_ThreadLocal
- Java基础:多线程之线程范围内的数据共享ThreadLocal
- Java并发库(五、六、七):线程范围内共享数据、ThreadLocal、共享数据的三种方法
- 黑马程序员-线程范围内数据共享
- Java线程与并发库高级应用-线程范围内共享数据ThreadLocal类
- 黑马程序员---java中如何实现线程范围内共享数据
- 多线程之线程范围内的数据共享ThreadLocal
- ThreadLocal类实现线程范围内数据共享
- ThreadLocal线程范围内共享数据
- 黑马程序员-线程范围内数据共享
- 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)
- ThreadLocal实现:java线程范围内的共享数据,线程外独立
- 线程范围内的共享数据 ThreadLocal
- 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)
- 4、线程范围内的数据共享之ThreadLocal