Example usage for java.lang ThreadLocal get

List of usage examples for java.lang ThreadLocal get

Introduction

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

Prototype

public T get() 

Source Link

Document

Returns the value in the current thread's copy of this thread-local variable.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ThreadLocal localThread = new ThreadLocal();
    Object o = localThread.get();
    localThread.set(o);//from  w ww. j a va  2s  .  c  o m

}

From source file:Main.java

public static void main(String[] args) {
    ThreadLocal<Integer> tlocal = new ThreadLocal<Integer>();
    tlocal.set(100);//from   www  .  jav  a 2  s  . c  om

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

From source file:Main.java

public static void main(String[] args) {

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

    tlocal.set(50);//from  ww w. j a v a2s.co  m
    System.out.println("value = " + tlocal.get());

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

From source file:Main.java

public static void main(String[] args) {

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

    tlocal.set(100);/*from w  ww.j a  v  a2s.c  om*/
    // returns the current thread's value
    System.out.println("value = " + tlocal.get());

}

From source file:Main.java

public static Object get(String key) {
    ThreadLocal tl = (ThreadLocal) THREAD_LOCAL_MAP.get(key);
    if (tl == null)
        return null;
    return tl.get();
}

From source file:Main.java

/**
 * Loops over all the possible date formats and tries to find the right one.
 *
 * @param value ISO date string/*from w  w  w  .j a va 2s .c o  m*/
 * @return Null if there is a parsing failure
 */
public static Date parseDate(String value) {
    if (value == null) {
        return null;
    }
    Date date = null;
    for (ThreadLocal<SimpleDateFormat> format : DATETIME_FORMATS) {
        try {
            date = format.get().parse(value);
            break;
        } catch (ParseException e) {
            // We loop through this until we found a valid one.
        }
    }
    return date;
}

From source file:Main.java

public synchronized static void append(String key, String msg) {
    ThreadLocal tl = (ThreadLocal) THREAD_LOCAL_MAP.get(key);
    if (tl == null) {
        tl = new ThreadLocal();
        THREAD_LOCAL_MAP.put(key, tl);/*from w ww . jav a 2  s.  c  om*/
    }
    StringBuffer buf = (StringBuffer) tl.get();
    if (buf == null) {
        buf = new StringBuffer();
        tl.set(buf);
    }
    buf.append(msg).append("\n");
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static void printObject(ThreadLocal threadLocal, Object obj) {
    String threadLocalCls = threadLocal.getClass().getName();
    if (threadLocal instanceof NamedThreadLocal) {
        System.out.print("......Spring threadlocal var: " + threadLocal + "=");
    } else if (obj == null || threadLocalCls.startsWith("java") || threadLocalCls.startsWith("sun")) {
        return;/*from   w w w. j a v a2  s  .c  o m*/
    }
    if (obj instanceof Object[]) {
        System.out.println(Arrays.deepToString((Object[]) obj));
    } else if (obj instanceof java.lang.ref.Reference) {
        java.lang.ref.Reference ref = (Reference) obj;
        System.out.println(" ref " + ref.getClass().getName() + " ref to " + ref.get());
    } else if (obj instanceof ThreadLocal) {
        ThreadLocal threadLc = (ThreadLocal) obj;
        String clssName = threadLc.getClass().getName();
        System.out.println(threadLc.getClass().getName() + "=" + threadLc.get());
    } else {
        System.out.println(obj);
    }
}

From source file:scriptella.driver.spring.EtlExecutorBean.java

/**
 * Return the bean factory associated with the current thread.
 * @return bean factory associated with the current thread.
 *//*  www. j  a v a 2s  . com*/
static synchronized BeanFactory getContextBeanFactory() {
    ThreadLocal threadLocal = getGlobalThreadLocal();
    BeanFactory f = (BeanFactory) threadLocal.get();
    if (f == null) {
        throw new IllegalStateException("No beanfactory associated with the current thread");
    }
    return f;
}

From source file:me.j360.dubbo.modules.util.text.HashUtil.java

private static MessageDigest get(ThreadLocal<MessageDigest> messageDigest) {
    MessageDigest instance = messageDigest.get();
    instance.reset();/* w  ww .  ja  v  a2 s  . c om*/
    return instance;
}