清单 4. 用 ThreadLocal 管理每线程调试日志 public class DebugLogger { private static class ThreadLocalList extends ThreadLocal { public Object initialValue() { return new ArrayList(); }
public List getList() { return (List) super.get(); } }
private ThreadLocalList list = new ThreadLocalList(); private static String[] stringArray = new String[0];
public void clear() { list.getList().clear(); }
public void put(String text) { list.getList().add(text); }
public String[] get() { return list.getList().toArray(stringArray); } }