[concurrency-interest] ThreadLocal.getIfPresent()

Peter Levart peter.levart at gmail.com
Wed Jun 6 15:07:51 EDT 2018

On 06/06/18 20:27, David Lloyd wrote:
> On Wed, Jun 6, 2018 at 1:17 PM, Peter Levart <peter.levart at gmail.com> wrote:
>> Or there could simply be the following method in ThreadLocal:
>> /**
>>   * @return true if current thread has a value present for this thread-local
>> variable; false if not.
>>   */
>> public boolean isPresent()
>> Would that be preferable?
> That would work assuming no ThreadLocal subclasses have such a method.
> Adding an instance method to a class that can be subclassed is always
> a risk in this way.

I think the risk is not so big because ThreadLocal instances are 
typically not publicly exposed. They are usually encapsulated and code 
accessing them "knows" the semantics of the isPresent() method if such 
method exists in a subclass. There is a danger though that such method 
on an existing subclass is not public and not private which would break 
binary link-ability if such public method was added to ThreadLocal...

The risk can be avoided entirely with a static method taking a 
ThreadLocal instance. But static method is not so nice-looking when 
called... :-(


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180606/7eb3b90d/attachment.html>

More information about the Concurrency-interest mailing list