/** * 一个ThreadLocal代表一个变量,故其中里只能放一个数据,有两个变量都要线程内共享,则要定义两个ThreadLocal. */ public class ThreadLocalTest { private static ThreadLocal<Integer> threadData = new ThreadLocal<Integer>(); public static void main(String[] args) { for (int i = 0; i <…
Java ThreadLocal is used to create thread local variables. We know that all threads of an Object share it's variables, so the variable is not thread safe. We can use synchronization for thread safety but if we want to avoid synchronization, we can us…