ThreadLocal, InheritableThreadLocal, Transmittable ThreadLocal, 스 레 드 변수 사본

87802 단어 자바
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>transmittable-thread-local</artifactId>
            <version>2.2.0</version>
 </dependency>
package com.jmdf.world;

import com.alibaba.ttl.TransmittableThreadLocal;

public class test01 {
   private static ThreadLocal a =new ThreadLocal();

    private static ThreadLocal b =new InheritableThreadLocal();

    private static  ThreadLocal d = new TransmittableThreadLocal();

    private static  int c = 0;

    public static void main(String[] args) {

        new Thread(() -> {
            a.set(1);
            b.set(1);
            c = 1;
            d.set(1);

            new Thread(() -> {
                System.out.println("     :1,       ");
                System.out.println("ThreadLocal------"+a.get());
                System.out.println("InheritableThreadLocal-------"+b.get());
                System.out.println("      -------------------"+c);
                System.out.println("TransmittableThreadLocal-------"+d.get());
            }).start();
        }).start();
    }
}

1,       
ThreadLocal------null
InheritableThreadLocal-------1
      -------------------1
TransmittableThreadLocal-------1

PublicTransmittableThreadLocal
package com.jmdf.world;

import com.alibaba.ttl.TransmittableThreadLocal;

import static java.lang.Thread.sleep;

public class PublicTransmittableThreadLocal {
    private static ThreadLocal tl = new TransmittableThreadLocal<>();

    public static void main(String[] args) {

        new Thread(() -> {

            String mainThreadName = "main_01";

            tl.set(1);

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            sleep(1L); //       tl.set      
            tl.set(2); //               ,         

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            System.out.println(String.format("    -%s,    =%s", Thread.currentThread().getName(), tl.get()));

        }).start();


        new Thread(() -> {

            String mainThreadName = "main_02";

            tl.set(3);

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            sleep(1L); //       tl.set      
            tl.set(4); //               ,         

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            new Thread(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            }).start();

            System.out.println(String.format("    -%s,    =%s", Thread.currentThread().getName(), tl.get()));

        }).start();

    }
    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

        (1),      -main_01,      -Thread-3,    =1
        (2),      -main_01,      -Thread-9,    =2
        (3),      -main_02,      -Thread-6,    =3
        (3),      -main_02,      -Thread-5,    =3
    -Thread-1,    =4
        (4),      -main_02,      -Thread-11,    =4
        (2),      -main_01,      -Thread-12,    =2
        (2),      -main_01,      -Thread-8,    =2
    -Thread-0,    =2
        (4),      -main_02,      -Thread-13,    =4
        (1),      -main_01,      -Thread-4,    =1
        (3),      -main_02,      -Thread-2,    =3
        (4),      -main_02,      -Thread-10,    =4
        (1),      -main_01,      -Thread-7,    =1

TestTransmittableThreadLocal
package com.jmdf.world;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestTransmittableThreadLocal {
    //       ,  TTL   ,          ,       TTL     (          )
    private static ExecutorService executorService = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(2));

    private static ThreadLocal tl = new TransmittableThreadLocal<>(); //    TTL   

    public static void main(String[] args) {

        new Thread(() -> {

            String mainThreadName = "main_01";

            tl.set(1);

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (1),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            sleep(1L); //       tl.set      
            tl.set(2); //               ,         

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (2),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            System.out.println(String.format("    -%s,    =%s", Thread.currentThread().getName(), tl.get()));

        }).start();


        new Thread(() -> {

            String mainThreadName = "main_02";

            tl.set(3);

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (3),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            sleep(1L); //       tl.set      
            tl.set(4); //               ,         

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            executorService.execute(() -> {
                sleep(1L);
                System.out.println(String.format("        (4),      -%s,      -%s,    =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
            });

            System.out.println(String.format("    -%s,    =%s", Thread.currentThread().getName(), tl.get()));

        }).start();

    }

    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

        (1),      -main_01,      -pool-1-thread-1,    =1
        (3),      -main_02,      -pool-1-thread-2,    =3
    -Thread-1,    =4
    -Thread-0,    =2
        (3),      -main_02,      -pool-1-thread-1,    =3
        (1),      -main_01,      -pool-1-thread-2,    =1
        (1),      -main_01,      -pool-1-thread-2,    =1
        (3),      -main_02,      -pool-1-thread-1,    =3
        (4),      -main_02,      -pool-1-thread-1,    =4
        (2),      -main_01,      -pool-1-thread-2,    =2
        (2),      -main_01,      -pool-1-thread-2,    =2
        (4),      -main_02,      -pool-1-thread-1,    =4
        (4),      -main_02,      -pool-1-thread-2,    =4
        (2),      -main_01,      -pool-1-thread-1,    =2

좋은 웹페이지 즐겨찾기