非线程安全 public class UnSafeThreadLocalDemo { private int count = 0; public static void main(String[] args) { UnSafeThreadLocalDemo unSafeThreadLocalDemo = new UnSafeThreadLocalDemo(); for (int i = 0; i < 5; i++) { int finalI = i; new Thread(new Runnabl…
1.ThreadLocal是什么 从名字我们就可以看到ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的.ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量. 2.ThreadLocal怎么用 public class TestTreadLocal { private static ThreadLocal<String> threadLocal = new ThreadLocal&…