Concurrent programming to explain (b)

● ThreadLocal introduce
ThreadLocal is called thread-local variables, and some place called thread local storage, the role of local variables ThreadLocal is provided within the thread, the thread variables play a role in the life cycle, reducing multiple threads within the same function or component some of the complexity of the delivery of common variables between.

Thread the ThreadLocal source parsing

 public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}
ThreadLocalMap getMap(Thread t) {
    return t.threadLocals;
}

void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}

Our method is to set an example, when the first call to the set method ThreadLocal thread is created when a member of threadLocals Thread objects inside. That ThreadLocal each set and get operations are carried out on the Thread of threadLocals operation, ThreadLocals manager threadLocals similar variables.
threadLocals similar to a special Map, it's key that threadLocal instance, while the value is the value we set the value.

● ThreadLocal Precautions
in an operating system, threads and processes is the maximum number of. In the operating system, the only condition to determine the uniqueness of threads and processes is the thread or process ID. Operating system at the time of recovery threads or processes, not necessarily to kill thread or process, the operation of the thread or process stack data, thread or process may be repeated use during busy times.

Use ThreadLocal, we must pay attention to recycling resources, before the end of each thread, the thread will save the current thread variable must be removed, call ThreadLocal.remove (), to leak does not occur. finally block run method.
In times of high concurrency, it may have a memory overflow.

● ThreadLocal Example

public class Test_ThreadLocal {

    volatile static String name = "zhangsan";
    static ThreadLocal<String> tl = new ThreadLocal<>();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                name = "lisi";
                tl.set("wangwu");
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 等待前一条线程运行结束
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name);
                System.out.println(tl.get());
            }
        }).start();

    }

}
Original articles published 0 · won praise 0 · Views 581

Guess you like

Origin blog.csdn.net/qq_41490913/article/details/105027046