ThreadLocal & Inheritable ThreadLocal에서 사용하는 구덩이
                                            
 2726 단어  다중 스레드
                    
           ,                       ,             run  。   ThreadLocal     ,    remove  。
  public class B {
     static final ThreadLocal threadParam = new ThreadLocal<>();
     public static void main(String[] args) throws InterruptedException {
         //        3   
         ExecutorService execService = Executors.newFixedThreadPool(3);
         //           
         while (true) {
             Thread t = new Thread(()->{
                     threadParam.set("abc");
                     System.out.println("t1:" + threadParam.get());
                     //     remove,      //                    threadParam.remove();
             });
             execService.execute(t);
             TimeUnit.SECONDS.sleep(1);
             Thread t2 = new Thread(()-> {
                     System.out.println("t2:" + threadParam.get());
             });
             execService.execute(t2);
         }
     } }
   public static void main(String[] args) throws InterruptedException {
        //        3   
        ExecutorService execService = Executors.newFixedThreadPool(3);
        //           
        while (true) {
            //  1,        
            Thread t = new Thread(()->{
                threadParam.set("abc");
                System.out.println("t1:" + threadParam.get());
                Thread t2 = new Thread(()->{
                    System.out.println("t2:" + threadParam.get());
//                        threadParam.remove();
                });
                execService.execute(t2);
                Thread t3 = new Thread(()->{
                    System.out.println("t3:" + threadParam.get());
//                        threadParam.remove();
                });
                execService.execute(t3);
                Thread t4 = new Thread(()->{
                    System.out.println("t4:" + threadParam.get());
//                        threadParam.remove();
                });
                execService.execute(t4);
            });
            execService.execute(t);
            TimeUnit.SECONDS.sleep(1);
            //  4,  1  
            Thread t5 = new Thread(()-> {
                threadParam.set("CBA");
                System.out.println("t5:" + threadParam.get());
            });
            execService.execute(t5);
        }
    }
  Another case see the below link: https://blog.csdn.net/Sunfj0821/article/details/81349775
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약 동료는 무심결에 이 문제를 제기하고 두 가지 방법을 직접 실천했다.물론 더 좋은 방법이 있을 거야. 방법 1 이런 방법은 비교적 흔히 볼 수 있는 해결 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.