class="java" name="code">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!"); } } }
?上面的代码中,在put函数中,只是把s加入到了list中,但是为什么store能取到这些内容,此处store.set(list)被注掉了,但是加到list里的数据怎么跑到store里面去了?