Example usage for java.lang ThreadLocal remove

List of usage examples for java.lang ThreadLocal remove

Introduction

In this page you can find the example usage for java.lang ThreadLocal remove.

Prototype

public void remove() 

Source Link

Document

Removes the current thread's value for this thread-local variable.

Usage

From source file:Main.java

public static void main(String[] args) {

    ThreadLocal<Integer> tlocal = new ThreadLocal<Integer>();

    tlocal.set(50);/*  ww w .jav  a2s  .  c  o  m*/
    System.out.println("value = " + tlocal.get());

    tlocal.remove();
    System.out.println("value = " + tlocal.get());
}

From source file:Main.java

public static void cleanThreadLocals() {
    for (ThreadLocal tl : locals) {
        tl.remove();
    }
}

From source file:Main.java

public static <T> void remove(ThreadLocal<T> threadLocal) {
    threadLocal.set(null);
    threadLocal.remove();
}

From source file:Main.java

public synchronized static void remove(String key) {
    ThreadLocal tl = (ThreadLocal) THREAD_LOCAL_MAP.get(key);
    if (tl != null) {
        tl.remove();
    }//from w w w .j  a va 2s . co m
}

From source file:Main.java

static final public void resetThreadLocal(Object lock) {
    synchronized (lock) {
        // Get a reference to the thread locals table of the current thread
        try {//  ww  w  .  j  a  va 2 s .com
            Thread thread = Thread.currentThread();

            threadLocalsField.setAccessible(true);
            Object threadLocalTable = threadLocalsField.get(thread);

            // Get a reference to the array holding the thread local variables inside the
            // ThreadLocalMap of the current thread

            tableField.setAccessible(true);
            Object table = tableField.get(threadLocalTable);

            // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object
            // is a reference to the actual ThreadLocal variable
            referentField.setAccessible(true);

            for (int i = 0; i < Array.getLength(table); i++) {
                // Each entry in the table array of ThreadLocalMap is an Entry object
                // representing the thread local reference and its value
                Object entry = Array.get(table, i);
                if (entry != null) {
                    // Get a reference to the thread local object and remove it from the table
                    ThreadLocal<?> threadLocal = (ThreadLocal<?>) referentField.get(entry);
                    threadLocal.remove();
                }
            }
        } catch (Exception e) {
        } //noop
        finally {
            try {
                if (referentField != null)
                    referentField.setAccessible(false);
                if (tableField != null)
                    tableField.setAccessible(false);
                if (threadLocalsField != null)
                    threadLocalsField.setAccessible(false);
            } catch (Throwable e) {
                // TODO: log error
            }
        }
    }
}

From source file:mondrian.util.UtilCompatibleJdk15.java

public <T> void threadLocalRemove(ThreadLocal<T> threadLocal) {
    threadLocal.remove();
}

From source file:org.apache.sling.resourceresolver.impl.CommonResourceResolverFactoryImpl.java

/**
 * Inform about a closed resource resolver.
 * Make sure to remove it from the current thread context.
 * @param resourceResolverImpl The resource resolver
 * @param ctrl The resource resolver control
 *//*from  www  .ja  v  a  2s  .co m*/
public void unregister(final ResourceResolver resourceResolverImpl, final ResourceResolverControl ctrl) {
    // close the context
    ctrl.close();
    // remove it from the set of weak references.
    refs.remove(ctrl.hashCode());

    // on shutdown, the factory might already be closed before the resolvers close
    // therefore we have to check for null
    final ThreadLocal<Stack<WeakReference<ResourceResolver>>> tl = resolverStackHolder;
    if (tl != null) {
        final Stack<WeakReference<ResourceResolver>> resolverStack = tl.get();
        if (resolverStack != null) {
            final Iterator<WeakReference<ResourceResolver>> i = resolverStack.iterator();
            while (i.hasNext()) {
                final WeakReference<ResourceResolver> ref = i.next();
                if (ref.get() == null || ref.get() == resourceResolverImpl) {
                    i.remove();
                }
            }
            if (resolverStack.isEmpty()) {
                tl.remove();
            }
        }
    }
}

From source file:org.codice.alliance.plugin.nitf.NitfPostProcessPlugin.java

private BufferedImage render(InputStream inputStream,
        Function<Pair<ImageSegment, NitfRenderer>, BufferedImage> imageSegmentFunction)
        throws InterruptedException, NitfFormatException {

    final ThreadLocal<BufferedImage> bufferedImage = new ThreadLocal<>();

    if (inputStream != null) {
        try {//  ww w.  j a v a2s  .co  m
            available.acquire();
            NitfRenderer renderer = nitfRendererSupplier.get();
            NitfParserInputFlow parserInputFlow = nitfParserSupplier.get();

            parserInputFlow.inputStream(inputStream).allData().forEachImageSegment(segment -> {
                if (bufferedImage.get() == null) {
                    BufferedImage bi = imageSegmentFunction.apply(new ImmutablePair<>(segment, renderer));
                    if (bi != null) {
                        bufferedImage.set(bi);
                    }
                }
            }).end();
        } finally {
            IOUtils.closeQuietly(inputStream);
            available.release();
        }
    }

    BufferedImage image = bufferedImage.get();
    bufferedImage.remove();
    return image;
}