当前位置:   article > 正文

ThreadLocal跨线程传递问题

ThreadLocal跨线程传递问题

  1. public class SysVisitRecordScheduler extends AbstractScheduler {
  2. private static ThreadLocal<List<String>> logHolder1 = new ThreadLocal<>();
  3. // private static final TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
  4. private static InheritableThreadLocal<List<String>> logHolder = new InheritableThreadLocal<>();
  5. @Override
  6. protected ProcessResult innerProcess(JobContext jobContext) {
  7. log.info("SysVisitRecordJob {} start execute", jobContext.getJobName());
  8. String noticeWorkNos = jobContext.getJobParameters();
  9. if (StringUtils.isBlank(noticeWorkNos)) {
  10. return new ProcessResult(true, "success");
  11. }
  12. try {
  13. return new ProcessResult(true, "success");
  14. } catch (Exception e) {
  15. log.error("SysVisitRecordJob execute fail: ", e);
  16. return new ProcessResult(false, e.getMessage());
  17. }
  18. }
  19. public static void main(String[] args) {
  20. logHolder.set(Lists.newArrayList("test"));
  21. List<String> list1 = logHolder.get();
  22. System.out.println(list1);
  23. CompletableFuture.runAsync(() -> {
  24. List<String> list2 = logHolder.get();
  25. System.out.println(list2);
  26. });
  27. }
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/447959
推荐阅读
相关标签
  

闽ICP备14008679号