[concurrency-interest] ThreadLocal.getIfPresent()

Peter Levart peter.levart at gmail.com
Wed Jun 6 14:17:41 EDT 2018



On 06/06/18 19:28, David Lloyd wrote:
> On Wed, Jun 6, 2018 at 12:09 PM, Peter Levart <peter.levart at gmail.com> wrote:
>> Di David,
>>
>> On 06/06/18 18:48, David Lloyd wrote:
>>
>> On Wed, Jun 6, 2018 at 10:17 AM, Peter Levart via Concurrency-interest
>> <concurrency-interest at cs.oswego.edu> wrote:
>>
>> Are you thinking about possible "inconsistency" between overridden get() and
>> not overridden getIfPresent() ? Like for example:
> Exactly.  You could possibly get around the issue by declaring it like this:
>
> public class ThreadLocal<T> {
>      // ...
>      public static <T> T getIfPresent(ThreadLocal<T> tl) {
>          return Thread.currentThread().hasThreadLocalInternal(tl) ?
> tl.get() : null;
>      }
>      // ...
> }
>

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?

Peter

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


More information about the Concurrency-interest mailing list