ThreadLocal的疑问

package com.threadlocal;

import java.util.LinkedList;
import java.util.List;

public class Warehouse {
	
	private static ThreadLocal<List<String>> store = new ThreadLocal<List<String>>(){
		@Override
		protected List<String> initialValue() {
			// TODO Auto-generated method stub
			return new LinkedList<String>(); 
		}
	}; 
    
    public void put(String s){  
        List<String> list = store.get();  
        list.add(s);  
//        store.set(list);
        System.out.println(Thread.currentThread().getName() + "放入: " + s);      
    }  
      
    public synchronized void get(){  
        List<String> list = store.get();  
        System.out.println(Thread.currentThread().getName() + "长度: " + list.size());  
        if(list != null){  
        	for(String temp : list){  
        		System.out.print(temp+" ");  
        	}
        	System.out.println();
        }else{  
        	System.err.println("ERROR!");  
        }      
    }  

}

 

你可能感兴趣的:(java,thread)