java ThreadLocal用法示例

直接上代码:

public class ThreadLocalStudy {

    private static ThreadLocal threadLocal = new ThreadLocal();

    private static Integer aa;

    public static void main(String[] args) throws InterruptedException {
        for(int i = 0; i<100; i++){
            new Thread(new Runnable() {
                public void run() {
                    for(int j =0 ; j <100 ; j++){
                        threadLocal.set(j);
                    }
                    System.out.println("thread: " + Thread.currentThread().getName() + " result: " + threadLocal.get());
                }
            }).start();
        }
    }
}

开启100个线程,每个线程对"同一个"变量进行赋值操作,输出结果如下

"C:\Program Files\Java\jdk1.8.0_71\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=59060:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_71\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_71\jre\lib\rt.jar;C:\software\titans-source\lock\target\classes" com.yxj.concurrent.ThreadLocalStudy
thread: Thread-1 result: 99
thread: Thread-3 result: 99
thread: Thread-0 result: 99
thread: Thread-2 result: 99
thread: Thread-6 result: 99
thread: Thread-4 result: 99
thread: Thread-5 result: 99
thread: Thread-10 result: 99
thread: Thread-7 result: 99
thread: Thread-12 result: 99
thread: Thread-13 result: 99
thread: Thread-14 result: 99
thread: Thread-17 result: 99
thread: Thread-9 result: 99
thread: Thread-8 result: 99
thread: Thread-11 result: 99
thread: Thread-24 result: 99
thread: Thread-15 result: 99
thread: Thread-18 result: 99
thread: Thread-16 result: 99
thread: Thread-21 result: 99
thread: Thread-22 result: 99
thread: Thread-23 result: 99
thread: Thread-20 result: 99
thread: Thread-25 result: 99
thread: Thread-26 result: 99
thread: Thread-27 result: 99
thread: Thread-29 result: 99
thread: Thread-30 result: 99
thread: Thread-28 result: 99
thread: Thread-31 result: 99
thread: Thread-32 result: 99
thread: Thread-33 result: 99
thread: Thread-34 result: 99
thread: Thread-19 result: 99
thread: Thread-35 result: 99
thread: Thread-36 result: 99
thread: Thread-37 result: 99
thread: Thread-38 result: 99
thread: Thread-39 result: 99
thread: Thread-41 result: 99
thread: Thread-40 result: 99
thread: Thread-42 result: 99
thread: Thread-43 result: 99
thread: Thread-44 result: 99
thread: Thread-50 result: 99
thread: Thread-45 result: 99
thread: Thread-48 result: 99
thread: Thread-47 result: 99
thread: Thread-52 result: 99
thread: Thread-46 result: 99
thread: Thread-49 result: 99
thread: Thread-53 result: 99
thread: Thread-51 result: 99
thread: Thread-59 result: 99
thread: Thread-55 result: 99
thread: Thread-56 result: 99
thread: Thread-54 result: 99
thread: Thread-57 result: 99
thread: Thread-60 result: 99
thread: Thread-81 result: 99
thread: Thread-58 result: 99
thread: Thread-61 result: 99
thread: Thread-63 result: 99
thread: Thread-62 result: 99
thread: Thread-65 result: 99
thread: Thread-66 result: 99
thread: Thread-69 result: 99
thread: Thread-70 result: 99
thread: Thread-93 result: 99
thread: Thread-67 result: 99
thread: Thread-72 result: 99
thread: Thread-68 result: 99
thread: Thread-73 result: 99
thread: Thread-99 result: 99
thread: Thread-77 result: 99
thread: Thread-78 result: 99
thread: Thread-75 result: 99
thread: Thread-76 result: 99
thread: Thread-79 result: 99
thread: Thread-82 result: 99
thread: Thread-83 result: 99
thread: Thread-85 result: 99
thread: Thread-84 result: 99
thread: Thread-88 result: 99
thread: Thread-91 result: 99
thread: Thread-92 result: 99
thread: Thread-89 result: 99
thread: Thread-90 result: 99
thread: Thread-64 result: 99
thread: Thread-94 result: 99
thread: Thread-95 result: 99
thread: Thread-97 result: 99
thread: Thread-71 result: 99
thread: Thread-96 result: 99
thread: Thread-98 result: 99
thread: Thread-80 result: 99
thread: Thread-86 result: 99
thread: Thread-87 result: 99
thread: Thread-74 result: 99

Process finished with exit code 0

总结: ThreadLocal为每个线程提供独立的数据副本,线程之间操作互不影响,从而保证线程安全.

你可能感兴趣的:(java,note)