Recently encountered a more subtle and simple problem, when using threadlocal found multiple threads in the string to string, to troubleshoot, to determine the problem is the thread pool, thread pools are reused, While threadlocal is using threads to do key, pay special attention to threadlocal when using the thread pool.
The threadlocal data is bound on the thread when the thread is created, so the workaround is to call remove () before using the data to remove the data generated by the previous thread
Workaround
Refactoring the Remove method
@Override
public void Remove () {
Super.remove ();
InitialValue ();
}
Remove first, and then initialize it again, so that the data is clean.
Of course you can also use remove in the call Finally, it is also possible.
When the threadlocal encounters a thread pool, the data between the threads interferes with each other and strings