Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread local remove method

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;}
};
like image 316
Sam Adamsh Avatar asked Nov 06 '12 18:11

Sam Adamsh


1 Answers

As per the javadoc for ThreadLocal:

... after a thread goes away, all of its copies of thread-local instances are subject to garbage collection ...

If the thread may still be around, and you want the resource dereferenced, better call remove().


Sounds like you might want to consider using a simple local variable if you want to clear it.

like image 81
Bohemian Avatar answered Nov 07 '22 12:11

Bohemian