[ https://issues.apache.org/jira/browse/LANG-792?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13225306#comment-13225306 ]
Gary D. Gregory commented on LANG-792: -------------------------------------- Howdy, A thread local is used so that calling reflectionToString on the same object (and any of its referred objects) from multiple threads works (returns the same String). If the registry was _not_ a thread local, then all threads would share the registry and that would lead to some threads not getting a proper toString of an object because another thread already registered it, erroneously thinking it was already visited. For example for an object o that has one ivar x which itself has a y ivar pointing to "y": Thread 1: registers x and does a deep toStrings on x Thread 2: checks that x is registered, it is, so it uses the default toString for x (not the deep toString) Thread 1: ungisters x ... Thread 1 gets x[y["y"]] OK Thread 2 gets x[y@123] BAD Also, with a thread local, the registry does not need to be a thread-safe data structure. > ToStringStyle registry may retain entries between invocations - is this a > problem? > ---------------------------------------------------------------------------------- > > Key: LANG-792 > URL: https://issues.apache.org/jira/browse/LANG-792 > Project: Commons Lang > Issue Type: Bug > Reporter: Sebb > > The class ToStringStyle maintains a registry in order to try to detect object > cycles (introduced by LANG-69). > Multiple instances in the same thread share the same registry (it's a > ThreadLocal - not clear why). > Entries can be left in the registry on return from calling various methods on > the instance. > Is this a bug? Can object cycles cause problems across method calls? > If it is intended for the registered objects to remain across method calls, > can objects from different instances interfere with each other? > The registry uses a WeakHashMap, so the entries should not cause problems for > garbage collection. > One solution would be to use an instance field to hold the map instead of > sharing them. > Would that use more memory, or be otherwise less efficient? -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira