二、深入解析原理
1 @RestController 2 @RequestMapping("/threadLocal") 3 public class ThreadLocalExample { 4 private ThreadLocal<Map<String,Student>> parentThreadLocal; 5 private ExecutorService executor; 6 private Gson gson = new Gson(); 7 private static final Logger logger = LoggerFactory.getLogger(ThreadLocalExample.class); 8 9 public ThreadLocalExample(){ 10 parentThreadLocal = new ThreadLocal<>(); 11 executor = Executors.newFixedThreadPool(2); 12 } 13 14 @GetMapping("") 15 public Map<String,Student> multi(){ 16 parentThreadLocal.set(Maps.newConcurrentMap()); 17 logger.info("value of parentThreadLocal in parentThread is [{}]",gson.toJson(parentThreadLocal.get())); 18 List<Future<Void>> threadResults = Lists.newArrayList(); 19 for(int i =0 ;i<=2;i++){ 20 threadResults.add(executor.submit(new SubThread("Thread"+i))); 21 } 22 23 for(Future<Void> future:threadResults){ 24 try { 25 future.get(); 26 } catch (InterruptedException | ExecutionException e) { 27 logger.error(e.getMessage(),e); 28 } 29 } 30 logger.info("value of parentThreadLocal in parentThread is [{}]",gson.toJson(parentThreadLocal.get())); 31 return parentThreadLocal.get(); 32 } 33 34 private final class SubThread implements Callable<Void>{ 35 private String identification; 36 37 public SubThread(String identification){ 38 this.identification = identification; 39 } 40 41 @Override 42 public Void call() throws Exception { 43 logger.info("start to execute thread[{}]",identification); 44 logger.info("value of parentThreadLocal in subThread is [{}]",gson.toJson(parentThreadLocal.get())); 45 Student subStudent = new Student(); 46 subStudent.setName(Thread.currentThread().getName()); 47 if(parentThreadLocal.get() != null){ 48 parentThreadLocal.get().put(identification, subStudent); 49 } 50 logger.info("end:value of parentThreadLocal in subThread is [{}]",gson.toJson(parentThreadLocal.get())); 51 return null; 52 } 53 } 54 }
1 2020-05-24T09:54:54.931+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.ThreadLocalExample.multi:38] - value of parentThreadLocal in parentThread is [{}] 2 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:64] - start to execute thread[Thread1] 3 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.ThreadLocalExample.call:64] - start to execute thread[Thread0] 4 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:65] - value of parentThreadLocal in subThread is [null] 5 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.ThreadLocalExample.call:65] - value of parentThreadLocal in subThread is [null] 6 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.ThreadLocalExample.call:71] - end:value of parentThreadLocal in subThread is [null] 7 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:71] - end:value of parentThreadLocal in subThread is [null] 8 2020-05-24T09:54:54.938+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:64] - start to execute thread[Thread2] 9 2020-05-24T09:54:54.939+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:65] - value of parentThreadLocal in subThread is [null] 10 2020-05-24T09:54:54.939+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.ThreadLocalExample.call:71] - end:value of parentThreadLocal in subThread is [null] 11 2020-05-24T09:54:54.939+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.ThreadLocalExample.multi:51] - value of parentThreadLocal in parentThread is [{}]
1 package com.example.demo.controller.threadlocalexample; 2 3 import java.util.Map; 4 import java.util.concurrent.CountDownLatch; 5 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import com.example.demo.dao.entity.Student; 11 import com.google.common.collect.Maps; 12 import com.google.gson.Gson; 13 14 import lombok.extern.slf4j.Slf4j; 15 16 /** 17 * <b>ThreadLocal 用法例子</b> 18 * <pre> 19 * 父线程定义的ThreadLocal变量里的value值不会被子线程使用,在示例中子线程给父线程 ThreadLocal变量设置值设置不了 20 * </pre> 21 * 22 */ 23 @RestController 24 @RequestMapping("/threadLocal") 25 @Slf4j 26 public class ThreadLocalExample { 27 /**ThreadLocal 变量***/ 28 private ThreadLocal<Map<String,Student>> parentThreadLocal; 29 private Gson gson = new Gson(); 30 31 public ThreadLocalExample(){ 32 parentThreadLocal = new ThreadLocal<>(); 33 } 34 35 @GetMapping 36 public Map<String,Map<String,Student>> multi(){ 37 parentThreadLocal.set(Maps.newConcurrentMap()); 38 log.info("value of parentThreadLocal in parentThread is [{}]",gson.toJson(parentThreadLocal.get())); 39 CountDownLatch latch = new CountDownLatch(3); 40 for(int i =0 ;i<=2;i++){ 41 new Thread(new SubThread("Thread"+i,latch)).start();; 42 } 43 try { 44 latch.await(); 45 } catch (InterruptedException e) { 46 log.error("CountDownLatch await wrongly.",e); 47 } 48 log.info("value of parentThreadLocal in parentThread is [{}]",gson.toJson(parentThreadLocal.get())); 49 Map<String,Map<String,Student>> resultMap = Maps.newHashMap(); 50 resultMap.put("parentThreadLocal", parentThreadLocal.get()); 51 log.info("main thread end.."); 52 return resultMap; 53 } 54 55 private final class SubThread implements Runnable{ 56 private ThreadLocal<Map<String,Student>> subThreadLocal; 57 private String identification; 58 private CountDownLatch latch; 59 60 public SubThread(String identification,CountDownLatch latch){ 61 this.identification = identification; 62 subThreadLocal = new ThreadLocal<Map<String,Student>>(); 63 /**在这里设置没有用,设置的话,父线程调用该构造函数时,还是在父线程执行, 64 subThreadLocal被放置在父线程的threadlocals变量,因此在run方法(子线程执行)通过get方法获取线程变量中存储的value是拿不到的 65 **/ 66 // subThreadLocal.set(Maps.newHashMap()); 67 this.latch = latch; 68 } 69 70 @Override 71 public void run() { 72 log.info("start to execute thread[{}]",identification); 73 subThreadLocal.set(Maps.newHashMap()); //sunThreadLocal 会被放置在子线程的threadLocals变量中 74 /*** 75 * 输出的值为null,并不能直接使用父线程中定义在parentThreadLocal中的值 76 */ 77 log.info("value of parentThreadLocal in subThread is [{}]",gson.toJson(parentThreadLocal.get())); 78 Student subStudent = new Student(); 79 subStudent.setName(identification); 80 if(parentThreadLocal.get() != null){ 81 parentThreadLocal.get().put(identification, subStudent); 82 }else { 83 log.error("cannot set value to parentThreadLocal,because parentThreadLocal has no value"); 84 } 85 subThreadLocal.get().put(identification, subStudent); 86 log.info("end:value of parentThreadLocal in subThread is [{}]",gson.toJson(parentThreadLocal.get())); 87 log.info("end:value of subThread in subThread is [{}]",gson.toJson(subThreadLocal.get())); 88 latch.countDown(); 89 } 90 } 91 }
运行结果:
2020-07-08T15:01:50.801+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.multi:38] - value of parentThreadLocal in parentThread is [{}] 2020-07-08T15:01:50.803+08:00 INFO [Thread-23] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:72] - start to execute thread[Thread1] 2020-07-08T15:01:50.803+08:00 INFO [Thread-22] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:72] - start to execute thread[Thread0] 2020-07-08T15:01:50.803+08:00 INFO [Thread-22] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:77] - value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.803+08:00 INFO [Thread-23] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:77] - value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.804+08:00 ERROR [Thread-22] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:83] - cannot set value to parentThreadLocal,because parentThreadLocal has no value 2020-07-08T15:01:50.804+08:00 ERROR [Thread-23] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:83] - cannot set value to parentThreadLocal,because parentThreadLocal has no value 2020-07-08T15:01:50.804+08:00 INFO [Thread-22] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:86] - end:value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.804+08:00 INFO [Thread-23] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:86] - end:value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.804+08:00 INFO [Thread-24] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:72] - start to execute thread[Thread2] 2020-07-08T15:01:50.804+08:00 INFO [Thread-22] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:87] - end:value of subThread in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T15:01:50.804+08:00 INFO [Thread-23] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:87] - end:value of subThread in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T15:01:50.805+08:00 INFO [Thread-24] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:77] - value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.806+08:00 ERROR [Thread-24] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:83] - cannot set value to parentThreadLocal,because parentThreadLocal has no value 2020-07-08T15:01:50.807+08:00 INFO [Thread-24] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:86] - end:value of parentThreadLocal in subThread is [null] 2020-07-08T15:01:50.807+08:00 INFO [Thread-24] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.run:87] - end:value of subThread in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T15:01:50.808+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.multi:48] - value of parentThreadLocal in parentThread is [{}] 2020-07-08T15:01:50.809+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.ThreadLocalExample.multi:51] - main thread end..
从上面的执行结果可看出:
父线程定义的 ThreadLocal 本地线程变量虽然设置了 Map 类型的 value 变量,但是子线程在获取这个 Map 类型 value 的时候获取不到,无法向其中设置值。
(2) 基本思路
1 package com.example.demo.controller.threadlocalexample; 2 3 import java.util.Map; 4 import java.util.concurrent.CountDownLatch; 5 import java.util.concurrent.ExecutorService; 6 import java.util.concurrent.Executors; 7 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 import com.example.demo.dao.entity.Student; 13 import com.google.common.collect.Maps; 14 import com.google.gson.Gson; 15 16 import lombok.extern.slf4j.Slf4j; 17 18 @RestController 19 @RequestMapping("/inheritablethreadLocal") 20 @Slf4j 21 public class InheritableThreadLocalExample { 22 /**线程池***/ 23 private ExecutorService executor; 24 private Gson gson = new Gson(); 25 /**InheritableThreadLocal 变量****/ 26 private InheritableThreadLocal<Map<String,Student>> parentInheritableThreadLocal; 27 28 public InheritableThreadLocalExample(){ 29 executor = Executors.newFixedThreadPool(2); 30 parentInheritableThreadLocal = new InheritableThreadLocal<>(); 31 } 32 /** 33 * <pre> 34 * <b>inheritablethreadLocal示例:父线程直接创建子线程使用,子线程使用inheritablethreadLocal中定义的value值</b><br> 35 * 通过示例可以发现,每个子线程都可以使用inheritablethreadLocal中定义的Map变量,并向其中设置数据,所有线程执行完毕,也可以在主线程中拿到子线程设置的数据<br> 36 * 原因为:<br> 37 * 父线程创建子线程的时候,会把父线程对象中的inheritablethreadLocals变量赋值给子线程对象中的inheritablethreadLocals变量中 38 * </pre> 39 */ 40 @GetMapping 41 public Map<String,Map<String,Student>> multi(){ 42 parentInheritableThreadLocal.set(Maps.newConcurrentMap()); 43 log.info("value of parentInheritableThreadLocal in parentThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 44 45 CountDownLatch latch = new CountDownLatch(3); 46 for(int i =0 ;i<=2;i++){ 47 new Thread(new SubThread("Thread"+i,latch)).start();; 48 } 49 try { 50 latch.await(); 51 } catch (InterruptedException e) { 52 log.error("CountDownLatch await wrongly.",e); 53 } 54 log.info("value of parentInheritableThreadLocal in parentThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 55 Map<String,Map<String,Student>> resultMap = Maps.newHashMap(); 56 resultMap.put("parentInheritableThreadLocal", parentInheritableThreadLocal.get()); 57 log.info("main thread end.."); 58 return resultMap; 59 } 60 61 /** 62 * <pre> 63 * <b>inheritablethreadLocal示例:父线程通过线程池创建子线程,子线程使用inheritablethreadLocal中定义的value值</b><br> 64 * 通过示例可以发现,每个子线程都可以使用inheritablethreadLocal中定义的Map变量,并向其中设置数据,所有线程执行完毕,也可以在主线程中拿到子线程设置的数据,<br> 65 * <br> 66 * 原因为:<br> 67 * 父线程创建子线程的时候,会把父线程对象中的inheritablethreadLocals变量赋值给子线程对象中的inheritablethreadLocals变量中 68 * </pre> 69 */ 70 @GetMapping("/pool") 71 public Map<String,Map<String,Student>> multiWithPool(){ 72 log.info("example of inheritablethreadLocal for subThread from Thread pool..."); 73 parentInheritableThreadLocal.set(Maps.newConcurrentMap()); 74 log.info("value of parentInheritableThreadLocal in parentThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 75 76 CountDownLatch latch = new CountDownLatch(3); 77 for(int i =0 ;i<=2;i++){ 78 executor.submit(new Thread(new SubThread("Thread"+i,latch))); 79 } 80 try { 81 latch.await(); 82 } catch (InterruptedException e) { 83 log.error("CountDownLatch await wrongly.",e); 84 } 85 log.info("value of parentInheritableThreadLocal in parentThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 86 Map<String,Map<String,Student>> resultMap = Maps.newHashMap(); 87 resultMap.put("parentInheritableThreadLocal", parentInheritableThreadLocal.get()); 88 log.info("main thread end.."); 89 return resultMap; 90 } 91 92 private final class SubThread implements Runnable{ 93 private ThreadLocal<Map<String,Student>> subThreadLocal; 94 private String identification; 95 private CountDownLatch latch; 96 97 public SubThread(String identification,CountDownLatch latch){ 98 this.identification = identification; 99 subThreadLocal = new ThreadLocal<Map<String,Student>>(); 100 /**在这里设置没有用,设置的话,父线程调用该构造函数时,还是在父线程执行, 101 subThreadLocal被放置在父线程的threadlocals变量,因此在run方法(子线程执行)通过get方法获取线程变量中存储的value是拿不到的 102 **/ 103 // subThreadLocal.set(Maps.newHashMap()); 104 this.latch = latch; 105 } 106 107 @Override 108 public void run() { 109 log.info("start to execute thread[{}]",identification); 110 subThreadLocal.set(Maps.newHashMap()); //sunThreadLocal 会被放置在子线程的threadLocals变量中 111 log.info("value of parentInheritableThreadLocal in subThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 112 Student subStudent = new Student(); 113 subStudent.setName(identification); 114 if(parentInheritableThreadLocal.get() != null){ 115 parentInheritableThreadLocal.get().put(identification, subStudent); 116 }else { 117 log.error("cannot set value to parentInheritableThreadLocal,because parentInheritableThreadLocal has no value"); 118 } 119 subThreadLocal.get().put(identification, subStudent); 120 log.info("end:value of parentInheritableThreadLocal in subThread is [{}]",gson.toJson(parentInheritableThreadLocal.get())); 121 log.info("end:value of subThreadLocal in subThread is [{}]",gson.toJson(subThreadLocal.get())); 122 latch.countDown(); 123 } 124 } 125 }
/inheritablethreadLocal 请求 运行结果:
2020-07-08T18:12:22.990+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multi:43] - value of parentInheritableThreadLocal in parentThread is [{}] 2020-07-08T18:12:22.991+08:00 INFO [Thread-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread0] 2020-07-08T18:12:22.992+08:00 INFO [Thread-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{}] 2020-07-08T18:12:22.992+08:00 INFO [Thread-11] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread1] 2020-07-08T18:12:22.993+08:00 INFO [Thread-12] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread2] 2020-07-08T18:12:22.993+08:00 INFO [Thread-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.993+08:00 INFO [Thread-11] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.994+08:00 INFO [Thread-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.994+08:00 INFO [Thread-11] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.995+08:00 INFO [Thread-12] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.995+08:00 INFO [Thread-11] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T18:12:22.997+08:00 INFO [Thread-12] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:22.998+08:00 INFO [Thread-12] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T18:12:22.999+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multi:54] - value of parentInheritableThreadLocal in parentThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:12:23.001+08:00 INFO [http-nio-8080-exec-10] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multi:57] - main thread end..
以上请求运行多少次,结果都如上图所示,是不变的。
/inheritablethreadLocal/pool (使用线程池来获取线程)请求运行结果:
服务启动的时候第一次执行结果:
2020-07-08T18:14:19.101+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:72] - example of inheritablethreadLocal for subThread from Thread pool... 2020-07-08T18:14:19.102+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:74] - value of parentInheritableThreadLocal in parentThread is [{}] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread0] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread1] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{}] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{}] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.105+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T18:14:19.105+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread2] 2020-07-08T18:14:19.104+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.105+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.106+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.106+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T18:14:19.107+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:85] - value of parentInheritableThreadLocal in parentThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:14:19.107+08:00 INFO [http-nio-8080-exec-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:88] - main thread end..
第二次执行结果:
2020-07-08T18:20:10.529+08:00 INFO [http-nio-8080-exec-3] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:72] - example of inheritablethreadLocal for subThread from Thread pool... 2020-07-08T18:20:10.529+08:00 INFO [http-nio-8080-exec-3] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:74] - value of parentInheritableThreadLocal in parentThread is [{}] 2020-07-08T18:20:10.530+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread1] 2020-07-08T18:20:10.530+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread0] 2020-07-08T18:20:10.530+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.530+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-1] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:109] - start to execute thread[Thread2] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:111] - value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.531+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:120] - end:value of parentInheritableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T18:20:10.532+08:00 INFO [pool-1-thread-2] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.run:121] - end:value of subThreadLocal in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T18:20:10.532+08:00 INFO [http-nio-8080-exec-3] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:85] - value of parentInheritableThreadLocal in parentThread is [{}] 2020-07-08T18:20:10.532+08:00 INFO [http-nio-8080-exec-3] [com.example.demo.controller.threadlocalexample.InheritableThreadLocalExample.multiWithPool:88] - main thread end..
从上面的执行结果可以看出:
由上图可看出,InheritableThreadLocal 重新写了 ThreadLocal 类的 createMap 方法 和 getMap 方法,直接使用 Thread 类中的 inheriableThreadLocals 属性来存储 从父线程中继承的 ThreadLocal 对象。 主要流程如下:
- 在父线程中定义全局线程本地变量为 InheritableThreadLocal 类型,父线程启动的时候,通过 InheritableThreadLocal 的 set 方法设置 value变量,该 线程本地变量(InheritableThreadLocal)会被放在父线程的 inheriableThreadLocals 的 table属性中,其中 key 为 InheritableThreadLocal对象, value 为 设置的 value变量。
- 父线程创建子线程的时候 会自动继承 父线程中的 inheriableThreadLocals ,放在子线程的 inheriableThreadLocals (它是一个ThreadLocalMap 类型 )中,见下面的 Thread 类中的红色区域部分。
Thread 类
private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) { if (name == null) { throw new NullPointerException("name cannot be null"); } this.name = name.toCharArray(); Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); if (g == null) { /* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter use the parent thread group. */ if (g == null) { g = parent.getThreadGroup(); } } /* checkAccess regardless of whether or not threadgroup is explicitly passed in. */ g.checkAccess(); /* * Do we have the required permissions? */ if (security != null) { if (isCCLOverridden(getClass())) { security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); } } g.addUnstarted(); this.group = g; this.daemon = parent.isDaemon(); this.priority = parent.getPriority(); if (security == null || isCCLOverridden(parent.getClass())) this.contextClassLoader = parent.getContextClassLoader(); else this.contextClassLoader = parent.contextClassLoader; this.inheritedAccessControlContext = acc != null ? acc : AccessController.getContext(); this.target = target; setPriority(priority); if (parent.inheritableThreadLocals != null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); /* Stash the specified stack size in case the VM cares */ this.stackSize = stackSize; /* Set thread ID */ tid = nextThreadID(); }
ThreadLocal#createInheritedMap 方法的实现 见下面
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { return new ThreadLocalMap(parentMap); }
ThreadLocal.ThreadLcoalMap#createInheritedMap
/** * Construct a new map including all Inheritable ThreadLocals * from given parent map. Called only by createInheritedMap. * * @param parentMap the map associated with parent thread. */ private ThreadLocalMap(ThreadLocalMap parentMap) { Entry[] parentTable = parentMap.table; int len = parentTable.length; setThreshold(len); table = new Entry[len]; for (int j = 0; j < len; j++) { Entry e = parentTable[j]; if (e != null) { @SuppressWarnings("unchecked") ThreadLocal<Object> key = (ThreadLocal<Object>) e.get(); if (key != null) { Object value = key.childValue(e.value); Entry c = new Entry(key, value); int h = key.threadLocalHashCode & (len - 1); while (table[h] != null) h = nextIndex(h, len); table[h] = c; size++; } } } }
通过以上方式达到子线程自动继承父线程使用的 线程本地变量,包括设置线程本地变量的 value 变量。
(3)结论
通过以上分析可以看出 创建子线程的时候 才会自动继承 父线程中的 线程本地变量。但是在实际使用的时候,父线程 和 子线程 我们都会使用线程池来进行管理,若父线程每次执行的时候,都会更改 线程本地变量 中的 value变量,则该值就不会被从线程池中获取到的线程继承。
1 package com.example.demo.controller.threadlocalexample; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.concurrent.Callable; 6 import java.util.concurrent.ExecutionException; 7 import java.util.concurrent.ExecutorService; 8 import java.util.concurrent.Executors; 9 import java.util.concurrent.Future; 10 11 import org.springframework.web.bind.annotation.GetMapping; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RestController; 14 15 import com.alibaba.ttl.TransmittableThreadLocal; 16 import com.alibaba.ttl.threadpool.TtlExecutors; 17 import com.example.demo.dao.entity.Student; 18 import com.google.common.collect.Lists; 19 import com.google.common.collect.Maps; 20 import com.google.gson.Gson; 21 22 import lombok.extern.slf4j.Slf4j; 23 24 /** 25 * 26 * 27 * 28 */ 29 @RestController 30 @RequestMapping("/transmittableThreadLocal") 31 @Slf4j 32 public class TransmittableThreadLocalExample { 33 /**线程池***/ 34 private ExecutorService executor; 35 private Gson gson = new Gson(); 36 /**TransmittableThreadLocal 变量****/ 37 private TransmittableThreadLocal<Map<String,Student>> parentTransmittableThreadLocal; 38 39 public TransmittableThreadLocalExample(){ 40 executor = Executors.newFixedThreadPool(2); 41 executor = TtlExecutors.getTtlExecutorService(executor); 42 parentTransmittableThreadLocal = new TransmittableThreadLocal<>(); 43 } 44 45 @GetMapping("/pool") 46 public Map<String,Map<String,Student>> multiWithPool(){ 47 parentTransmittableThreadLocal.set(Maps.newConcurrentMap()); 48 log.info("value of parentTransmittableThreadLocal in parentThread is [{}]",gson.toJson(parentTransmittableThreadLocal.get())); 49 List<Future<Void>> threadResults = Lists.newArrayList(); 50 for(int i =0 ;i<=2;i++){ 51 threadResults.add(executor.submit(new SubThread("Thread"+i))); 52 } 53 54 for(Future<Void> future:threadResults){ 55 try { 56 future.get(); 57 } catch (InterruptedException | ExecutionException e) { 58 log.error(e.getMessage(),e); 59 } 60 } 61 62 log.info("value of parentTransmittableThreadLocal in parentThread is [{}]",gson.toJson(parentTransmittableThreadLocal.get())); 63 Map<String,Map<String,Student>> resultMap = Maps.newHashMap(); 64 resultMap.put("parentTransmittableThreadLocal", parentTransmittableThreadLocal.get()); 65 return resultMap; 66 } 67 68 private final class SubThread implements Callable<Void>{ 69 private TransmittableThreadLocal<Map<String,Student>> subThreadLocal; 70 private String identification; 71 72 public SubThread(String identification){ 73 this.identification = identification; 74 subThreadLocal = new TransmittableThreadLocal<Map<String,Student>>(); 75 subThreadLocal.set(Maps.newHashMap()); 76 } 77 78 @Override 79 public Void call() throws Exception { 80 log.info("start to execute thread[{}]",identification); 81 log.info("value of parentTransmittableThreadLocal in subThread is [{}]",gson.toJson(parentTransmittableThreadLocal.get())); 82 Student subStudent = new Student(); 83 subStudent.setName(identification); 84 parentTransmittableThreadLocal.get().put(identification, subStudent); 85 subThreadLocal.get().put(identification, subStudent); 86 87 log.info("end:value of parentTransmittableThreadLocal in subThread is [{}]",gson.toJson(parentTransmittableThreadLocal.get())); 88 log.info("end:value of subThread in subThread is [{}]",gson.toJson(subThreadLocal.get())); 89 return null; 90 } 91 } 92 }
运行结果:
服务启动的时候第一次执行结果:
2020-07-08T19:02:06.380+08:00 INFO [http-nio-8080-exec-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.multiWithPool:48] - value of parentTransmittableThreadLocal in parentThread is [{}] 2020-07-08T19:02:06.390+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread0] 2020-07-08T19:02:06.390+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread1] 2020-07-08T19:02:06.390+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{}] 2020-07-08T19:02:06.390+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{}] 2020-07-08T19:02:06.393+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:02:06.393+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:02:06.394+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T19:02:06.394+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T19:02:06.394+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread2] 2020-07-08T19:02:06.394+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:02:06.394+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:02:06.395+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T19:02:06.395+08:00 INFO [http-nio-8080-exec-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.multiWithPool:62] - value of parentTransmittableThreadLocal in parentThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}]
第二次执行结果:
2020-07-08T19:06:12.590+08:00 INFO [http-nio-8080-exec-5] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.multiWithPool:48] - value of parentTransmittableThreadLocal in parentThread is [{}] 2020-07-08T19:06:12.591+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread0] 2020-07-08T19:06:12.591+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread1] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{}] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.592+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:80] - start to execute thread[Thread2] 2020-07-08T19:06:12.593+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:81] - value of parentTransmittableThreadLocal in subThread is [{"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.593+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:87] - end:value of parentTransmittableThreadLocal in subThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}] 2020-07-08T19:06:12.593+08:00 INFO [pool-2-thread-1] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread2":{"name":"Thread2"}}] 2020-07-08T19:06:12.594+08:00 INFO [pool-2-thread-2] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.call:88] - end:value of subThread in subThread is [{"Thread1":{"name":"Thread1"}}] 2020-07-08T19:06:12.595+08:00 INFO [http-nio-8080-exec-5] [com.example.demo.controller.threadlocalexample.TransmittableThreadLocalExample.multiWithPool:62] - value of parentTransmittableThreadLocal in parentThread is [{"Thread2":{"name":"Thread2"},"Thread1":{"name":"Thread1"},"Thread0":{"name":"Thread0"}}]
从上面的执行结果可以看出:
说明:
- TransmittableThreadLocal 继承了 InheritableThreadLocal 类;
- 新增静态变量 或 方法,其中 静态变量 holder 用来存储 线程中所有 TransmittableThreadLocal 类型的本地线程变量 (除了holder本身);静态方法 doExecuteCallback 在当前 TransmittableThreadLocal 变量的Value值要赋给 子线程之后或之前执行(可能是一些特殊逻辑);
- TransmittableThreadLocal 类中存在 Transmitter 类,该类提供的多个方法 在 将父线程中的 TransmittableThreadLocal 本地线程变量和 Value值过渡给运行的子线程 起到关键作用。
说明:
- 入口主类为 TtlExecutors ,定义的 jdk 提供的 Executor 和 ExecutorService 类型的变量都传递给 TtlExecutors 中的方法,方法会对传递的变量包装下生成对应的 ExecutorTtlWrapper 或 ExecutorServiceTtlWrapper 变量;
- 当程序向 ExecutorTtlWrapper 或 ExecutorServiceTtlWrapper 提交 runnable 或 callable 对象时,装饰类会 将 runnable 或 callable 对象 转换成 TtlRunable 或 TtlCallable 对象;
- 父线程的 TransmittableThreadLocal 本地线程变量传递给 运行中的子线程 主要在 执行 TtlRunable 或 TtlCallable 对象 中的 run/call 方法完成;
说明:
- 父线程使用 TransmittableThreadLocal 方法中的 get 或 set 方法均会将 TransmittableThreadLocal 本地线程变量存储到 父线程中的 inheritableThreadLocals 属性中的 holder(TransmittableThreadLocal变量)对应的WeakHashMap中,值为null;
- 对 Java 提供的 ExectorService 做了 Wrapper 封装;
- 在执行 分装后的 ExectorService 对象 的submit方式时,会自动 对 定义的 runnable 或 Callable 对象 Wrapper,而在Wrapper 的同时,就自动 将父线程中的 inheritableThreadLocals 属性中的 holder 中所有 TransmittableThreadLocal变量设置其在父线程中的 Vaue 值并存储在封装后的 runnable 或 Callable 对象 中;
- 而执行 分装后的 runnable 或 Callable 对象 call方法时,会执行向其中增加的逻辑 ---- 从 分装后的 runnable 或 Callable 对象 中拿取从父线程中获取到的 holder 并全部设置到子线程中,这样子线程中与父线程 同样的 transmittableThreadLocal 变量对应 Value 值就是父线程中;
通过以上方式 达到子线程执行之前自动继承父线程所有本地线程变量的 Value 变量。