[concurrency-interest] ThreadLocal.getIfPresent()

David Lloyd david.lloyd at redhat.com
Wed Jun 6 13:28:28 EDT 2018


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;
    }
    // ...
}

-- 
- DML


More information about the Concurrency-interest mailing list