I
Ian Pilcher
I have a method which searches a singly linked list for an object. It
needs to return references to both the object and the preceding object
in the list. I have created a very simple inner class to encapsulate
this information:
private final class MemberReference
{
final Member referent;
final Member previous;
MemberReference(Member referent, Member previous)
{
this.referent = referent;
this.previous = previous;
}
}
An instance of this class will be required for just about every access
the the class, including reads, which could cause a lot of these objects
to be created and then discarded.
I'd like to avoid this if possible, but the class does need to support
reading by multiple threads, so I wouldn't want to use a single
instance (or a couple of class fields). I'm thinking of using a
ThreadLocal, but I can't help wondering if retrieving the thread-local
value might not be more expensive than creating a new object.
Anyone have any experience/thoughts?
Thanks!
needs to return references to both the object and the preceding object
in the list. I have created a very simple inner class to encapsulate
this information:
private final class MemberReference
{
final Member referent;
final Member previous;
MemberReference(Member referent, Member previous)
{
this.referent = referent;
this.previous = previous;
}
}
An instance of this class will be required for just about every access
the the class, including reads, which could cause a lot of these objects
to be created and then discarded.
I'd like to avoid this if possible, but the class does need to support
reading by multiple threads, so I wouldn't want to use a single
instance (or a couple of class fields). I'm thinking of using a
ThreadLocal, but I can't help wondering if retrieving the thread-local
value might not be more expensive than creating a new object.
Anyone have any experience/thoughts?
Thanks!