Java 8 introduced ThreadLocal.withInitial
which is a simpler alternative to creating an anonymous inner class to initialise a
ThreadLocal
instance.
This rule raises an issue when a ThreadLocal
anonymous inner class can be replaced by a call to
ThreadLocal.withInitial
.
Noncompliant code example
ThreadLocal<List<String>> myThreadLocal =
new ThreadLocal<List<String>>() { // Noncompliant
@Override
protected List<String> initialValue() {
return new ArrayList<String>();
}
};
Compliant solution
ThreadLocal<List<String>> myThreadLocal = ThreadLocal.withInitial(ArrayList::new);