是的,用户将看到 Context 对象,因为对它的引用存储在 HttpSession 中。即使 ThreadLocal 中的引用为空,它仍然会在第二次请求期间在会话中找到。
编辑:在ThreadLocal 的 OpenJDK 源代码中(从第 410 行开始),您可以看到 ThreadLocal 的 set 和 remove 方法之间的区别。调用 set(null) 将使 ThreadLocalMap 条目保留为空值,而 remove() 将完全删除它。它不会对您的问题产生影响,会话中仍然会引用您的 Context 对象。
When I first read your question title I interpreted it differently because there was no mention of the HttpSession or clearing the ThreadLocal. Maybe this confused some of the responders. It sounded like you wanted to know if a ThreadLocal variable set in the first request (and not cleared) would still be accessible in the second request. I think the answer is that this depends on how your webserver handles threads. If there is a threadpool of 10 threads that are randomly reused you should have a 10% chance of finding the same ThreadLocal variable in the second request.