1. 程式人生 > >ThreadLocal實現線程範圍內的共享變量

ThreadLocal實現線程範圍內的共享變量

run etag dom tar todo tint thread 數據 setname

ThreadLocal變量自動與當前線程相關聯,替代Map,HashMap結構:

package cn.qy.heima2;

import java.util.Random;

public class ThreadLocalTest 
{
    private static ThreadLocal<Integer> xLocal= new ThreadLocal<Integer>();
    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 int data=new Random().nextInt(); System.out.println(Thread.currentThread().getName()
+"has put data:"+data); xLocal.set(data); //xLocal與當前線程相關,進行數據存放 new A().get(); new B().get(); } }).start(); } } static
class A { public void get() { int data=xLocal.get(); //xLocal與當前線程相關,進行數據取出 System.out.println("A from"+Thread.currentThread().getName()+"get data:"+data); } } static class B { public void get() { int data=xLocal.get(); System.out.println("B from"+Thread.currentThread().getName()+"get data:"+data); } } }

將多個共享變量封裝為一個對象放入ThreadLocal變量裏面:

package cn.qy.heima2;
import java.util.Random;
public class ThreadLocalTest 
{
    private static ThreadLocal<Integer> xLocal= new ThreadLocal<Integer>();
    private static ThreadLocal<MyThreadScopeData> mythreadScopedata=new ThreadLocal<MyThreadScopeData>();
    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
                            int data=new Random().nextInt();                
                            System.out.println(Thread.currentThread().getName()
                                    +"has put data:"+data);        
                            xLocal.set(data);   //xLocal與當前線程相關,進行數據存放
                            MyThreadScopeData mydata= new MyThreadScopeData();
                            mydata.setName("qianyong"+data);
                            mydata.setAge(data);
                            mythreadScopedata.set(mydata);
                            new A().get();
                            new B().get();
                        }
                    }).start(); 
    }
    }
static class A
{
public void get()
{
int data=xLocal.get();       //xLocal與當前線程相關,進行數據取出
System.out.println("A from"+Thread.currentThread().getName()+"get data:"+data);
MyThreadScopeData mydata=mythreadScopedata.get();
System.out.println("A from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());
}
}
static class B
{
public void get()
{
int data=xLocal.get();
System.out.println("B from"+Thread.currentThread().getName()+"get data:"+data);
MyThreadScopeData mydata=mythreadScopedata.get();
System.out.println("B from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());
}
}
}

class 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-0has put data:-2141728227
Thread-1has put data:1848865996
A fromThread-1get data:1848865996
A fromThread-1,get myName:qianyong1848865996,get myAge:1848865996
A fromThread-0get data:-2141728227
A fromThread-0,get myName:qianyong-2141728227,get myAge:-2141728227
B fromThread-1get data:1848865996
B fromThread-1,get myName:qianyong1848865996,get myAge:1848865996
B fromThread-0get data:-2141728227
B fromThread-0,get myName:qianyong-2141728227,get myAge:-2141728227

通過ThreadLocal變量創建與某一線程相關聯的單例對象:

package cn.qy.heima2;
import java.util.Random;
public class ThreadLocalTest 
{
    private static ThreadLocal<Integer> xLocal= new ThreadLocal<Integer>();
    private static ThreadLocal<MyThreadScopeData> mythreadScopedata=new ThreadLocal<MyThreadScopeData>();
    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
                            int data=new Random().nextInt();                
                            System.out.println(Thread.currentThread().getName()
                                    +"has put data:"+data);        
                            xLocal.set(data);   //xLocal與當前線程相關,進行數據存放
                    /*MyThreadScopeData mydata= new MyThreadScopeData();
                            mydata.setName("qianyong"+data);
                            mydata.setAge(data);
                            mythreadScopedata.set(mydata);*/
                            MyThreadScopeData.getThreadInstance().setName("qianyong"+data);
                            MyThreadScopeData.getThreadInstance().setAge(data);
                            new A().get();
                            new B().get();
                        }
                    }).start(); 
    }
    }
static class A
{
public void get()
{
int data=xLocal.get();         //xLocal與當前線程相關,進行數據取出
System.out.println("A from"+Thread.currentThread().getName()+",get data:"+data);
/*MyThreadScopeData mydata=mythreadScopedata.get();
System.out.println("A from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());*/
MyThreadScopeData mydata=MyThreadScopeData.getThreadInstance();
System.out.println("A from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());
}
}
static class B
{
public void get()
{
int data=xLocal.get();
System.out.println("B from"+Thread.currentThread().getName()+",get data:"+data);
/*MyThreadScopeData mydata=mythreadScopedata.get();
System.out.println("B from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());*/
MyThreadScopeData mydata=MyThreadScopeData.getThreadInstance();
System.out.println("B from"+Thread.currentThread().getName()+",get myName:"+mydata.getName()+",get myAge:"+mydata.getAge());
}
}
}

class MyThreadScopeData
{
private MyThreadScopeData() {};
public static /*synchronized*/ MyThreadScopeData getThreadInstance()
{
    MyThreadScopeData instance=map.get();
    if(instance==null)
    {
        instance = new MyThreadScopeData();
        map.set(instance);
    }
    return instance;
}
//private static MyThreadScopeData instance=null;  
//創建單例,饑漢模式   //new MyThreadScopeData(); 創建單例,飽漢模式

private static ThreadLocal<MyThreadScopeData> map=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;
}
}

ThreadLocal實現線程範圍內的共享變量