Tuesday, February 14, 2012

ThreadLocal Best Practices

  • Be careful to initialize the ThreadLocal object whenever the thread starts working, or you might end up connecting a logged-off user's data to another user's thread. When you're using ThreadLocals in a situation in which a filter initializes the object with user data, make sure there are no other entries to your servlet application—except through the filter.
  • Always remember that the ThreadLocal data is connected to the thread, not the user or anything else. A good idea is to delete the ThreadLocal data when it's no longer used.
  • Use InheritableThreadLocal to pass parent ThreadLocal data to child threads.


References

No comments:

Post a Comment