标签:void ack 代码块 -- pre 区别 start 现象 模块
ThreadLocal的作用和目的:用于实现线程内的数据共享,即对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,而在另外线程中运行时又共享另外一份数据。
举一个反面例子,当我们使用简单的int类型存储线程间共享的数据,但在另外一个线程我们想共享另外一份数据,此时就会造成数据混淆的现象,如下:
package com.zzj.test; import java.util.Random; public class Test { private static int data; public static void main(String[] args) { for(int i = 1; i <= 2; i ++) { new Thread(() -> { data = new Random().nextInt(); System.out.println(Thread.currentThread().getName() + " has get data: " + data); new A().get(); new B().get(); }).start(); } } private static class A{ public void get() { System.out.println("A from " + Thread.currentThread().getName() + " has get data: " + data); } } private static class B{ public void get() { System.out.println("B from " + Thread.currentThread().getName() + " has get data: " + data); } } }
运行结果如下:
而当我们使用ThreadLocal时,就不会出现数据混淆的现象:
public class Test { private static ThreadLocal<Integer> tl = new ThreadLocal<>(); public static void main(String[] args) { for(int i = 1; i <= 2; i ++) { new Thread(() -> { int data = new Random().nextInt(); System.out.println(Thread.currentThread().getName() + " has get data: " + data); tl.set(data); new A().get(); new B().get(); }).start(); } } private static class A{ public void get() { int data = tl.get(); System.out.println("A from " + Thread.currentThread().getName() + " has get data: " + data); } } private static class B{ public void get() { int data = tl.get(); System.out.println("B from " + Thread.currentThread().getName() + " has get data: " + data); } } }
结果如下:
我们找到ThreadLocal的源码,看看其基本运行原理:
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this); } public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); } // 通过静态内部类实现变量与线程绑定 static class ThreadLocalMap {...}
ThreadLocal基本运行过程:每个线程调用全局ThreadLocal对象的set方法,就相当于往其内部的map中增加一条记录,key是各自的线程对象,value是各自的set方法传进去的值。在线程结束时可以调用ThreadLocal.clear()方法,这样会更快地释放内存,不调用在线程结束后也会自动释放相关的ThreadLocal变量。
结论--我们结合源码和上述两个例子可以看出:
标签:void ack 代码块 -- pre 区别 start 现象 模块
原文地址:https://www.cnblogs.com/yimengxianzhi/p/12366290.html