I want to fly higher
          programming Explorer
          posts - 114,comments - 263,trackbacks - 0
              直接看代碼示例
              
          package com.book.java7.concurrency;

          import java.time.LocalDate;
          import java.util.concurrent.TimeUnit;

          /**
           * {@link InheritableThreadLocal}示例
           * 
           * <pre>
           * public class InheritableThreadLocal<T> extends ThreadLocal<T> {
           *     protected T childValue(T parentValue) {
           *         return parentValue;
           *     }
           * </pre>
           * 
           * <pre>
           *     childValue方法是在子線程初始化時調用的,可(Thread.dump).將【子線程-childValue】和date綁定.
           *  子線程調用date.get時, 會取到子線程初始化時放到ThreadLocalMap的值.
           * </pre>
           * 
           * <p>
           * landon認為其實是用線程局部變量這個類更多的原因可能在于為了隱藏線程內部的一些實現細節,使用戶外部調用更簡單,完全不必理會線程內部的一些東西!
           * 
           * @author landon
           *
           
          */

          public class InheritableThreadLocalExample {

              
          private static class Task implements Runnable {

                  
          // InheritableThreadLocal局部變量
                  private static InheritableThreadLocal<LocalDate> date = new InheritableThreadLocal<LocalDate>() {
                      
          // 初始化
                      protected LocalDate initialValue() {
                          
          return LocalDate.now();
                      }


                      
          // 首先輸出繼承的值.然后改變子線程局部變量的值(加了一天)
                      protected LocalDate childValue(LocalDate parentValue) {
                          
          // Thread.dumpStack();

                          System.out.println(String.format("call childValue Thread:%s,parentValue:%s", Thread.currentThread()
                                  .getName(), parentValue));

                          
          return parentValue.plusDays(1);
                      }

                  }
          ;

                  @Override
                  
          public void run() {
                      System.out.println(String.format("%s:task begin.date:%s", Thread.currentThread().getName(), date.get()));

                      
          try {
                          TimeUnit.SECONDS.sleep(1);
                      }
           catch (InterruptedException e) {
                          e.printStackTrace();
                      }


                      
          // 創建一個子線程
                      
          // 從輸出可以看到:此時輸出的是childValue改變后的值
                      Thread subThread = new Thread(Thread.currentThread().getName() + "-sub"{
                          @Override
                          
          public void run() {
                              System.out.println(String.format("%s:task execute.date:%s", Thread.currentThread().getName(),
                                      date.get()));
                          }

                      }
          ;

                      subThread.start();

                      System.out.println(String.format("%s:task end.date:%s", Thread.currentThread().getName(), date.get()));
                  }

              }


              
          public static void main(String[] args) throws Exception {
                  Task task = new Task();

                  
          for (int i = 0; i < 3; i++{
                      Thread t 
          = new Thread(task, "Main-Sub-" + i);
                      t.start();

                      Thread.sleep(
          1000);
                  }

              }

          }

          posted on 2014-11-25 16:22 landon 閱讀(6148) 評論(0)  編輯  收藏 所屬分類: Program
          主站蜘蛛池模板: 武威市| 邵阳县| 邯郸县| 平顶山市| 九寨沟县| 云林县| 华坪县| 三穗县| 麻栗坡县| 灵璧县| 松阳县| 阜康市| 韶山市| 东丰县| 广汉市| 精河县| 上高县| 葫芦岛市| 武冈市| 剑河县| 晴隆县| 通榆县| 武义县| 凤山市| 丽江市| 化德县| 右玉县| 云龙县| 淳化县| 正镶白旗| 北川| 灵宝市| 汉寿县| 襄垣县| 阆中市| 辉南县| 金湖县| 古交市| 昆明市| 云龙县| 德化县|