ThreadLocal类及应用技巧_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > ThreadLocal类及应用技巧

ThreadLocal类及应用技巧

 2014/7/4 0:30:24  xuweilovejava  程序员俱乐部  我要评论(0)
  • 摘要:importjava.util.HashMap;importjava.util.Map;importjava.util.Random;publicclassThreadLocalTest{privatestaticMap<Thread,Integer>threadData=newHashMap<Thread,Integer>();privatestaticThreadLocal<Integer>threadLocal=newThreadLocal<
  • 标签:thread 应用 技巧
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadLocalTest {
private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>();
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
/**
* 开启两个线程
* 第一个线程数据共享
* 第二个线程数据共享
* @param args
*/
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);
MyThreadScopeData.getInstance().setName("name"+data);
MyThreadScopeData.getInstance().setAge(data);

new A().get();
new B().get();
}
}).start();
}



}

static class A {
public void get() {
int data = threadData.get(Thread.currentThread());
System.out.println("A1 " + Thread.currentThread().getName()
+ " has put data :" + data);
int data2 = threadLocal.get();
System.out.println("A2 " + Thread.currentThread().getName()
+ " has put data :" + data2);
MyThreadScopeData myThreadScopeData = MyThreadScopeData.getInstance();
System.out.println("A3 " + Thread.currentThread().getName()
+ " name :" + myThreadScopeData.getName() +" , age :" + myThreadScopeData.getAge() );

}
}

static class B {
public void get() {
int data = threadData.get(Thread.currentThread());
System.out.println("B " + Thread.currentThread().getName()
+ " has put data :" + data);
int data2 = threadLocal.get();
System.out.println("B2 " + Thread.currentThread().getName()
+ " has put data :" + data2);

MyThreadScopeData myThreadScopeData = MyThreadScopeData.getInstance();
System.out.println("B3 " + Thread.currentThread().getName()
+ " name :" + myThreadScopeData.getName() +" , age :" + myThreadScopeData.getAge() );

}
}

}

class MyThreadScopeData{
/*//饱汉模式
private MyThreadScopeData(){}
public static MyThreadScopeData getInstance(){
return myThreadScopeData;
}
private static MyThreadScopeData myThreadScopeData= new MyThreadScopeData();*/

/*//恶汉模式
private MyThreadScopeData(){}
public static synchronized MyThreadScopeData getInstance(){
if(myThreadScopeData==null)
myThreadScopeData = new MyThreadScopeData();
return myThreadScopeData;
}
private static MyThreadScopeData myThreadScopeData = null;*/

private MyThreadScopeData(){}
public static synchronized MyThreadScopeData getInstance(){
MyThreadScopeData instance = myThreadScopeData.get();
if(instance==null){
instance = new MyThreadScopeData();
myThreadScopeData.set(instance);
}
return instance;
}

private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();

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;
}

}


运行结果:
Thread-0 has put data :-412573948
Thread-1 has put data :-589943191
A1 Thread-1 has put data :-589943191
A1 Thread-0 has put data :-412573948
A2 Thread-1 has put data :-589943191
A2 Thread-0 has put data :-412573948
A3 Thread-1 name :name-589943191 , age :-589943191
A3 Thread-0 name :name-412573948 , age :-412573948
B Thread-1 has put data :-589943191
B Thread-0 has put data :-412573948
B2 Thread-1 has put data :-589943191
B2 Thread-0 has put data :-412573948
B3 Thread-1 name :name-589943191 , age :-589943191
B3 Thread-0 name :name-412573948 , age :-412573948

上一篇: Java 中新增的 foreach 的用法 下一篇: 没有下一篇了!
发表评论
用户名: 匿名