// Note: This does have the values problem (complicated values can cause memory leaks) sclass BetterThreadLocal { Map map = newWeakHashMap(); *() {} *(A value) { set(value); } bool isSet() { ret map.containsKey(currentThread()); } A get() { if (map.containsKey(currentThread())) ret map.get(currentThread()); A value = initialValue(); set(value); ret value; } A get(Thread thread) { ret thread == null ? null : map.get(thread); } void set(A a) { map.put(currentThread(), a); } public A initialValue() { null; } }