@@ -0,0 +1,2614 @@
+package com.njust.test.learn;%0A%0Aimport java.util.Random;%0A%0Aimport lombok.Getter;%0Aimport lombok.Setter;%0Aimport lombok.ToString;%0A%0A/**%0A * ThreadLocal%0A * %3Cpre%3E%0A *%0A * %3C/pre%3E%0A */%0Apublic class ThreadLocalDemo%0A%7B%0A    //private static Map%3CThread, Integer%3E dataMap = new HashMap%3C%3E();%0A    private static ThreadLocal%3CInteger%3E threadLocal = new ThreadLocal%3C%3E();%0A    %0A    public static void main(String%5B%5D args)%0A    %7B%0A        for (int i = 0; i %3C 2; i++)%0A        %7B%0A            new Thread(() -%3E %7B%0A                int data = new Random().nextInt();%0A                //simple:1%0A                threadLocal.set(data);%0A                System.out.println(Thread.currentThread().getName() + %22 put data:%22 + data);%0A                %0A                //complicated:2%0A                Student instance = StudentHolder.getInstance();%0A                instance.setName(%22Name%22 + data);%0A                instance.setAge(data);%0A                %0A                new A().get();%0A                new B().get();%0A            %7D).start();%0A        %7D%0A    %7D%0A    %0A    static class A%0A    %7B%0A        public void get()%0A        %7B%0A            //1%0A            Integer data = threadLocal.get();%0A            System.out.println(Thread.currentThread().getName() + %22 A read data:%22 + data);%0A            %0A            //2%0A            Student instance = StudentHolder.getInstance();%0A            System.out.println(Thread.currentThread().getName() + %22 A read student:%22 + instance);%0A        %7D%0A    %7D%0A    %0A    static class B%0A    %7B%0A        public void get()%0A        %7B%0A            //1%0A            Integer data = threadLocal.get();%0A            System.out.println(Thread.currentThread().getName() + %22 B read data:%22 + data);%0A            %0A            //2%0A            Student instance = StudentHolder.getInstance();%0A            System.out.println(Thread.currentThread().getName() + %22 B read student:%22 + instance);%0A        %7D%0A    %7D%0A    %0A%7D%0A%0Aclass StudentHolder%0A%7B%0A    private static ThreadLocal%3CStudent%3E studentThreadLocal = new ThreadLocal%3C%3E();%0A    %0A    /*%0A    * %E6%88%96%E8%80%85%E8%BF%99%E6%A0%B7%E5%86%99%EF%BC%8C%E5%B0%B1%E4%B8%8D%E4%BC%9A%E5%87%BA%E7%8E%B0 studentThreadLocal.get() == null%E7%9A%84%E6%83%85%E5%86%B5%0A    private static ThreadLocal%3CStudent%3E studentThreadLocal = new ThreadLocal%3CStudent%3E()%0A    %7B%0A        @Override%0A        protected Student initialValue()%0A        %7B%0A            return new Student();%0A        %7D%0A    %7D;%0A    */%0A    %0A    public static Student getInstance()%0A    %7B%0A        Student student = studentThreadLocal.get();%0A        if (student == null)%0A        %7B%0A            student = new Student();%0A            studentThreadLocal.set(student);%0A        %7D%0A        return student;%0A    %7D%0A%7D%0A%0A@Getter%0A@Setter%0A@ToString%0Aclass Student%0A%7B%0A    private String name;%0A    %0A    private int age;%0A%7D%0A%0A%0A%0A

Create ThreadLocalDemo.java