Do you need to call remove on a thread local if you are not deploying in a server environment, even if the app uses a cached thread pool?
public static ThreadLocal<Integer> i = new ThreadLocal<Integer>(){{
public Integer initialValue(){return 3;}
};
In theory, if both the
ThreadandThreadLocalare still in play, the value will be reachable. If either are unreachable, in theory, the value is garbage collectible if otherwise unreferenced. However, there is a bug in OpenJDK which means if the value references theThreadLocal(surprisingly common) and theThreadis still running, it’ll leak.So yes, in a sense, there is nothing special about a server environment. However, generally where you are repeated reloading code during development then there it often the case that the
ThreadLocalis reachable from the value (value to class-of-the-value to class-loader-of-the-class to all-classes-ever-loaded-through-class-loader to static-fields-of-those-classes to aThreadLocal). Implementations of Java Beans and JDBC may have similar issues.