ThreadLocal 的原理和实例

ThreadLocal 是java开源中非常流行的线程管理工具类,它能防止并发并且能提高效率,应该如果用synchronized,效率实在太差了。
看了它的源码,我们就很容易知道它的原理了:
public class ThreadLocal
{
 private Map values = Collections.synchronizedMap(new HashMap());
 public Object get()
 {
  Thread curThread = Thread.currentThread(); 
  Object o = values.get(curThread); 
  if (o == null && !values.containsKey(curThread))
  {
   o = initialValue();
   values.put(curThread, o); 
  }
  return o; 
 }

 public void set(Object newValue)
 {
  values.put(Thread.currentThread(), newValue);
 }

 public Object initialValue()
 {
  return null; 
 }
} 


注意这个initialValue方法,我们一般是要重写这个方法的。

下面是实例代码。

public class StringThread extends Thread{
    private ThreadLocal tl;
    private String threadName;
    public StringThread(ThreadLocal tl, String threadName){
    	this.tl=tl;
    	this.threadName=threadName;
    }
	@Override
	public void run() {
		if(this.tl!=null){
			for(int i=0;i<3;i++){
				StringBuffer sb=(StringBuffer)this.tl.get();
				sb.append("run");
				System.out.println(this.threadName+":"+this.tl.get());
			}
			
		}
	}
    
}


public class ThreadLocalTest {
	private ThreadLocal tl=new ThreadLocal(){
		@Override
		public Object get() {
			return super.get();
		}
		@Override
		protected Object initialValue() {
			return new StringBuffer();
		}
		@Override
		public void remove() {
			super.remove();
		}
		@Override
		public void set(Object arg0) {
			super.set(arg0);
		}
		
	};
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ThreadLocalTest tlt=new ThreadLocalTest();
		StringThread st1=new StringThread(tlt.tl,"thread 1 ");
		StringThread st2=new StringThread(tlt.tl,"thread 2 ");
		StringThread st3=new StringThread(tlt.tl,"thread 3 ");
		
		st1.start();
		st2.start();
		st3.start();
	}

}


运行结果是:
thread 3 :run
thread 2 :run
thread 1 :run
thread 2 :runrun
thread 3 :runrun
thread 2 :runrunrun
thread 1 :runrun
thread 3 :runrunrun
thread 1 :runrunrun

你可能感兴趣的:(threadLocal)