InheritableThreadLocal sample code
程序员文章站
2022-07-14 12:45:42
...
The following code print null. If using InheritableThreadLocal, it print main.
class A {
//static ThreadLocal local = new InheritableThreadLocal();
static ThreadLocal local = new ThreadLocal();
}
public class Sample {
public static void main (String [] args) {
A.local.set("main");
Thread t = new Thread() {
@Override
public void run() {
System.out.println(A.local.get());
}
};
t.start();
}
}
The following code shows the usage of childValue method.
class A {
static ThreadLocal<String> local = new InheritableThreadLocal<String>() {
@Override
protected String childValue(String parentValue) {
return "child: " + parentValue;
}
};
}
public class Sample {
public static void main (String [] args) {
A.local.set("main");
Thread t = new Thread() {
@Override
public void run() {
System.out.println(A.local.get());
}
};
t.start();
}
}
上一篇: 宅在家太无聊了,我用Python做了一个能作弊的抽奖程序
下一篇: GitHub中GPG认证使用
推荐阅读
-
C#生成Code39条形码而非条形码字体的方法
-
解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYSQL-python/
-
Entity Framework Code First属性映射约定
-
记录微信支付开发中的小经验(errcode = 40163; errmsg = "code been used")
-
利用Typings为Visual Studio Code实现智能提示功能
-
全新Visual Studio Code预览版0.10.10发布下载
-
Let's Code
-
ubuntu下VS code如何调试C++代码
-
详解VS Code使用之Vue工程配置format代码格式化
-
CKEditor 4.4.1 添加代码高亮显示插件功能教程【使用官方推荐Code Snippet插件】