Example usage for java.lang ThreadLocal ThreadLocal

List of usage examples for java.lang ThreadLocal ThreadLocal

Introduction

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

Prototype

public ThreadLocal() 

Source Link

Document

Creates a thread local variable.

Usage

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 o  m*/

    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 w  w  .  ja  va  2 s .  c  o  m
    // returns the current thread's value
    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);/* w  w w.  j  a v a2 s.  c  om*/
    System.out.println("value = " + tlocal.get());

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

From source file:Main.java

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

}

From source file:Main.java

public static boolean isInTimeNow(int start, int end) {
    Calendar calendar = new ThreadLocal<Calendar>() {
        protected Calendar initialValue() {
            return Calendar.getInstance();
        }//from   w  w  w  . j ava  2 s.c om
    }.get();
    long current = System.currentTimeMillis();
    calendar.setTimeInMillis(current);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    long originToday = calendar.getTimeInMillis();
    long distance = current - originToday;
    if (start <= distance && distance <= end) {
        return true;
    }

    return false;
}

From source file:Main.java

static <T> ThreadLocal<T> createThreadLocal() {
    ThreadLocal<T> tl = new ThreadLocal<>();
    locals.add(tl);
    return tl;
}

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 .ja  v 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

private static ThreadLocal<XPath> getXPath() {
    if (xPath == null) {
        xPath = new ThreadLocal<XPath>() {
            @Override//from  ww w.j a  va 2 s  .  c o m
            protected XPath initialValue() {
                return XPathFactory.newInstance().newXPath();
            }
        };
    }
    return xPath;
}

From source file:Main.java

public synchronized static void put(String key, Object value) {
    ThreadLocal tl = (ThreadLocal) THREAD_LOCAL_MAP.get(key);
    if (tl == null) {
        tl = new ThreadLocal();
        THREAD_LOCAL_MAP.put(key, tl);//from w  w w .  j  a  v a  2s.c  om
    }
    tl.set(value);
}

From source file:org.wings.session.SessionManager.java

/**
 * Lazy and synchronized initialize the ThreadLocal to avoid race-conditions.
 *
 * @return ThreadLocal possibly holding the current session if available.
 *//* w ww .  j a  v  a  2 s .  com*/
private static synchronized ThreadLocal<Session> getCurrentSession() {
    if (currentSession == null) {
        currentSession = new ThreadLocal<Session>();
    }

    return currentSession;
}