- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设置了所有线程通用的ThreadLocal初始值。当调用构造函数时,我更改了值并且它打印正确。但是当启动线程时,它又变成了初始值?这是预期的吗?如果是,解释是什么?下面是我的两个类。提前致谢。
MyRunnableThreadLocal.java
public class MyRunnableThreadLocal implements Runnable {
private ThreadLocal<String> threadLocal = new ThreadLocal<String>(){
@Override protected String initialValue() {
return "Thread Name not set yet";
}
};
public MyRunnableThreadLocal(String threadName) {
System.out.println(threadLocal.get());
threadLocal.set(threadName);
System.out.println("Thread name: " + threadLocal.get());
}
@Override
public void run() {
System.out.println(threadLocal.get());
threadLocal.set(threadLocal.get() + " " + (Math.random() * 100D));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(threadLocal.get());
}
}
ThreadLocalTest.java
public class ThreadLocalTest {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnableThreadLocal("Thread 1"));
Thread t2 = new Thread(new MyRunnableThreadLocal("Thread 2"));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
Thread Name not set yet
Thread name: Thread 1
Thread Name not set yet
Thread name: Thread 2
Thread Name not set yet
Thread Name not set yet
Thread Name not set yet 20.24825634584746
Thread Name not set yet 59.67633602373702
最佳答案
ThreadLocal 是运行代码的线程的本地对象(以及与其关联的线程本地对象。)
Thread Name not set yet <= runs in main
Thread name: Thread 1 <= runs in main for the first MyRunnableThreadLocal object
Thread Name not set yet <= runs in main for the 2nd MyRunnableThreadLocal object
Thread name: Thread 2 <= runs in main for the 2nd MyRunnableThreadLocal object
Thread Name not set yet <= runs in the first thread for the first MyRunnableThreadLocal
Thread Name not set yet <= runs in the 2nd thread for the 2nd MyRunnableThreadLocal
Thread Name not set yet 20.24825634584746 <= runs in the first thread for the first MyRunnableThreadLocal
Thread Name not set yet 59.67633602373702 <= runs in the 2nd thread for the 2nd MyRunnableThreadLocal
所以你有三个线程和两个ThreadLocal。您的 main 函数同时使用本地线程,并且您启动的两个线程各有一个值。
每次有一个新的 ThreadLocal 对象或一个新的线程时,它都有一个新的初始值。
What about setting the thread local value?
在创建另一个 ThreadLocal 或在另一个线程中使用它之前,您只能读取一次设置的值。
If first four lines are run in main thread, then set in the line# 2 is not reflecting on line# 3?
第 3 行是不同 MyRunnableThreadLocal
中的不同 ThreadLocal
对象
If it is working on first MyRunnableThreadLocal object then why it is not reflecting on line# 5 or 6?
第 5 行和第 6 行是这些 ThreadLocal 第一次在这些线程中使用,因此它们具有初始值。
注意:如果您使用InheritableThreadLocal
,它将执行您所期望的操作。这是因为新线程“继承”其父线程设置的值。
关于java - ThreadLocal InitialValue 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105234/
我一直在读这个article关于 Tomcat 中的 ThreadLocal 泄漏。第一个示例包含以下代码: public class MyCounter { private int
天真地,我希望 ThreadLocal 是 Thread 到值类型的某种 WeakHashMap。所以当我了解到一个 ThreadLocal 的值实际上是 saved in a map in the
一 简单例子 1 代码 package concurrent.threadlocal; /** * ThreadLocal测试 * * @author cakin */ public class T
并发编程-ThreadLocal 说在前面的话 今天的文章很短,但是很经典,值得你仔细阅读每一个文字… 正如我开篇所说,我们要整理一些java并发编程的学习文档,这一篇就是第七篇:ThreadLoca
最近接了一个新需求,业务场景上需要在原有基础上新增2个字段,接口新增参数意味着很多类和方法的逻辑都需要改变,需要先判断是否属于该业务场景,再做对应的逻辑。原本的打算是在入口处新增变量,在操作数据的时
前言 ThreadLocal为变量在每个线程中都创建了一个副本,所以每个线程可以访问自己内部的副本变量,不同线程之间不会互相干扰。本文会基于实际场景介绍ThreadLocal如何使用以及内部实现机
来源:blog.csdn.net/mycs2012/article/details/90898128 1、FastThreadLocal的引入背景和原理简介 2、实现源码分析 2.1、Unpadded
来源:blog.csdn.net/mycs2012/article/details/90898128 1、FastThreadLocal的引入背景和原理简介 2、实现源码分析 2.1、Unpadded
.Net 4. ThreadLocal<> 实现 IDisposable。但似乎调用 Dispose() 实际上并没有释放对所持有的线程本地对象的引用。 这段代码重现了这个问题: using Syst
在类(class)ReentrantReadWriteLock以下是奇怪的评论: transient ThreadLocalHoldCounter readHolds; Sync() { re
如果我们有一个 ThreadLocal 属性(每个线程都有其唯一的属性),那么哪个是正确的(我们不想使用自动 setter/getter): A) private ThreadLocal _someP
我正在浏览 ThreadLocal类文档,想知道它可以在什么场景下使用。 首先我认为它可以用于那些我们有第三方/遗留类并且我们想要处理同步问题的场景。然后我查看了 ThreadLocal 的其他示例,
我最近读了一篇关于 Equation Group's Sophisticated Hacking 的文章确凿的证据是一个常量,也出现在 JDK 8 源代码中,例如ThreadLocal.java HA
我的基于 ThreadLocal 的类遇到问题。任何帮助,将不胜感激。这是一个带有简单列表的基类: public class ThreadLocalTest { protected static fi
使用ThreadLocal类编程时,字段应该声明为final吗?如果编写类似 private ThreadLocal threadLocal 的代码稍后在构造函数中初始化它,因为变量 threadLo
我有以下类(class) 这个类用来保存我所有的ThreadLocal数据成员 public class ThreadLocalManager { public static final Th
在一次工作 session 上。我听说Thread Local绝对是一种反模式,因为新的应用程序服务器使用称为新IO的新线程技术。事实上,他们告诉我ThreadLocal的问题是一个完整的线程必须等待
ThreadLocal 是否会自动清除为已完成的线程创建的值? 最佳答案 是的,这些变量可用于垃圾收集,但前提是没有其他对这些值的引用(由其他线程持有)。但是当你说一个线程完成时,它不应该像一个线程池
我意识到 ThreadLocal 已被多次访问,尤其是 SimpleDateFormat 示例。 但似乎即使将 SDF 设置为“ThreadLocal”,我们仍然为每个线程创建一个 SDF() 实例,
在 JSR 315 中添加了对 servlet 和过滤器的异步支持。在这样的过滤器中创建的 ThreadLocal 线程安全吗? ResourceFilter com.app.fil
我是一名优秀的程序员,十分优秀!