static ThreadLocal tl = new ThreadLocal() {
protected Object initialValue() {
System.err.println("initialValue called");
ArrayList list = new ArrayList(COUNT);
for (int i = 0; i < COUNT; i++) {
MyThreadLocal mtl = new MyThreadLocal();
mtl.get();
list.add(mtl);
}
return list;
}
};
public static void main(String[] args) throws Throwable {
Object first = tl.get();
Object second = tl.get();
Object third = tl.get();
System.err.println("first=" + first);
System.err.println("second=" + second);
System.err.println("second=" + third);
}
static class MyThreadLocal extends ThreadLocal {
protected Object initialValue() {
return Boolean.TRUE;
}
}
}
---------- END SOURCE ----------
(我会将此添加为 Fakrudeen 答案的评论,但我没有足够的点:-) )
I was investigating the same usage of synchronized on ThreadLocal initialValue(). Fakrudeen's answer includes a link to a Java 1.5 defect in which the synchronized keyword was the work around for multiple objects being created. It was fixed in Java 1.6
If you run this test (taken from the Fakrudeen's link) in Java 1.5 and compare your results to a later version, you will see that in versions 1.6 and later that synchronized is not necessary.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
public class ThreadLocalBug {
static int COUNT = 8;
static ThreadLocal tl = new ThreadLocal() {
protected Object initialValue() {
System.err.println("initialValue called");
ArrayList list = new ArrayList(COUNT);
for (int i = 0; i < COUNT; i++) {
MyThreadLocal mtl = new MyThreadLocal();
mtl.get();
list.add(mtl);
}
return list;
}
};
public static void main(String[] args) throws Throwable {
Object first = tl.get();
Object second = tl.get();
Object third = tl.get();
System.err.println("first=" + first);
System.err.println("second=" + second);
System.err.println("second=" + third);
}
static class MyThreadLocal extends ThreadLocal {
protected Object initialValue() {
return Boolean.TRUE;
}
}
}
---------- END SOURCE ----------
(I would have added this as a comment to Fakrudeen's answer, but I don't have enough points :-) )
发布评论
评论(3)
我正在研究 ThreadLocalinitialValue() 上同步的相同用法。 Fakrudeen 的答案包括一个 Java 1.5 缺陷的链接,其中同步关键字是创建多个对象的解决方法。它已在 Java 1.6 中修复。
如果您在 Java 1.5 中运行此测试(取自 Fakrudeen 的链接)并将结果与更高版本进行比较,您将发现在版本 1.6 及更高版本中不需要同步。
---------- 开始源 ----------
导入java.util.ArrayList;
公共类 ThreadLocalBug {
静态 int 计数 = 8;
}
---------- END SOURCE ----------
(我会将此添加为 Fakrudeen 答案的评论,但我没有足够的点:-) )
I was investigating the same usage of synchronized on ThreadLocal initialValue(). Fakrudeen's answer includes a link to a Java 1.5 defect in which the synchronized keyword was the work around for multiple objects being created. It was fixed in Java 1.6
If you run this test (taken from the Fakrudeen's link) in Java 1.5 and compare your results to a later version, you will see that in versions 1.6 and later that synchronized is not necessary.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
public class ThreadLocalBug {
static int COUNT = 8;
}
---------- END SOURCE ----------
(I would have added this as a comment to Fakrudeen's answer, but I don't have enough points :-) )
由于此错误,它可能很有用:
https://bugs.openjdk.java.net/browse/JDK-6550283?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab
It may be useful due to this bug:
https://bugs.openjdk.java.net/browse/JDK-6550283?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab
不,这完全没有意义,而且很可能是由不知道自己在做什么的人写的。
No, that's completely pointless, and was likely written by someone who didn't know what they were doing.