[ 
https://issues.apache.org/jira/browse/LANG-792?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13225361#comment-13225361
 ] 

Sebb commented on LANG-792:
---------------------------

bq. If the registry was not a thread local, then all threads would share the 
registry

That is only true if the ToStringStyle (TSS) instance is shared between threads.

I had assumed that each ToStringBuilder would use its own TSS instance, but 
that is not the case.
Looking again at the code, I see that the ToStringStyle (TSS) classes seem to 
be intended to be used as singletons which are then shared between threads. 

That explains why the ThreadLocal is needed.

Also, I now realise that the earlier problem with the test cases failing on 
Gump is that some of them created a builder but then failed to use the 
toString() method on it. That left entries in the registry, and caused the 
subsequent test to fail if it checked that the registry was empty.

In normal use, the toString() method is required; the registry will then be 
cleaned up.
The test cases need to do so too; I'll fix them up again.
                
> 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

        

Reply via email to