ThreadLocal的synchronized的get和initialValues方法

发布于 2024-12-07 02:34:57 字数 229 浏览 0 评论 0 原文

我遇到了一段代码,其中 ThreadLocal 已同步。

我找不到同步这些方法的任何用处。如果我错了请纠正我。

-克里希纳。

I came across a code where the get() and initialValue() methods of ThreadLocal is synchronized.

I couldn't find any use of having these methods synchronized. Correct me if I am wrong.

-Krishna.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2024-12-14 02:34:57

我正在研究 ThreadLocalinitialValue() 上同步的相同用法。 Fakrudeen 的答案包括一个 Java 1.5 缺陷的链接,其中同步关键字是创建多个对象的解决方法。它已在 Java 1.6 中修复。

如果您在 Java 1.5 中运行此测试(取自 Fakrudeen 的链接)并将结果与​​更高版本进行比较,您将发现在版本 1.6 及更高版本中不需要同步。

---------- 开始源 ----------
导入java.util.ArrayList;

公共类 ThreadLocalBug {
静态 int 计数 = 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 ----------

(我会将此添加为 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 :-) )

彩扇题诗 2024-12-14 02:34:57

不,这完全没有意义,而且很可能是由不知道自己在做什么的人写的。

No, that's completely pointless, and was likely written by someone who didn't know what they were doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文