Java SE API
ThreadLocal cleanup
Remove request context after use to avoid unintended retention.
Formal Syntax & Structure
java syntaxjava
try { ... } finally { context.remove(); }Working Example
Example Snippetjava
public class Main {
public static void main(String[] args) throws Exception { var context = new ThreadLocal<String>(); try { context.set("synthetic-request"); System.out.println(context.get()); } finally { context.remove(); } System.out.println(context.get() == null); }}