gpt4 book ai didi

java - 将 ThreadLocal 与包含静态成员的现有类一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:36:35 25 4
gpt4 key购买 nike

我正在尝试使用 ThreadLocal 为预先存在的非线程安全类提供线程安全,但遇到了问题。似乎没有执行隔离 - 线程仍然共享静态,而不是每个线程的本地静态。

我相信我的用法几乎与 this StackOverflow question 中描述的 SimpleDateFormatter 的示例本地化完全平行。 ,但它并没有按照我希望的方式进行。

我所希望的是,有人使用过它会指出我必须犯的极其愚蠢的错误......所以我想我的问题是:你能发现我在这里做错了什么吗?

这是我的简单类:

public class SimpleClassWithStaticMembers {
private static String theStaticString =
"StaticStringInClassWithStaticMember";
public void setTheStaticString (String val) {
SimpleClassWithStaticMembers.theStaticString = val;
}
public String getTheStaticString () {
return SimpleClassWithStaticMembers.theStaticString;
}
}

这是创建 SimpleClassWithStaticMembers 线程本地实例的线程类:

public class SimpleTesterThread extends Thread {
private void showMsg (String msg) {
System.out.println (msg);
System.out.flush();
}
public SimpleTesterThread (String threadId) {
super(threadId);
}
public void run() {
try { Thread.sleep(2000); } catch (InterruptedException ex) { }
ThreadLocal<SimpleClassWithStaticMembers> localizedClass =
new ThreadLocal<SimpleClassWithStaticMembers>();
localizedClass.set(new SimpleClassWithStaticMembers());
// repeating here to be sure we overlap all with all
for (int ii=0; ii < 3; ii++) {
localizedClass.get().setTheStaticString ("Setby_" + this.getName());
try { Thread.sleep(2000); } catch (InterruptedException ex) { }
showMsg(" Thread [" + this.getName() + "] - "
+ localizedClass.get().getTheStaticString() + "'.");
}
showMsg ("Thread [" + this.getName()
+ "] complete. Our 'threadlocal' string is now - "
+ localizedClass.get().getTheStaticString() + "'.");
localizedClass.remove();
}
}

SimpleTesterThread 的十个实例被创建(给它们不同的线程名称,如“AAAAAAAAAA”、“BBBBBBBBB”等),然后启动时,输出清楚地表明它们正在共享实例。日志输出包括:

...            Thread [JJJJJJJJJJ] - Setby_CCCCCCCCCC'.            Thread [DDDDDDDDDD] - Setby_JJJJJJJJJJ'.            Thread [IIIIIIIIII] - Setby_DDDDDDDDDD'.            Thread [GGGGGGGGGG] - Setby_IIIIIIIIII'.            Thread [EEEEEEEEEE] - Setby_GGGGGGGGGG'.Thread [EEEEEEEEEE] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.            Thread [HHHHHHHHHH] - Setby_GGGGGGGGGG'.            Thread [BBBBBBBBBB] - Setby_GGGGGGGGGG'.            Thread [FFFFFFFFFF] - Setby_GGGGGGGGGG'.Thread [FFFFFFFFFF] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'....            Thread [JJJJJJJJJJ] - Setby_GGGGGGGGGG'.Thread [JJJJJJJJJJ] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.Thread [HHHHHHHHHH] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.            Thread [GGGGGGGGGG] - Setby_GGGGGGGGGG'.Thread [GGGGGGGGGG] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.            Thread [IIIIIIIIII] - Setby_GGGGGGGGGG'.            Thread [CCCCCCCCCC] - Setby_GGGGGGGGGG'.Thread [CCCCCCCCCC] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.Thread [AAAAAAAAAA] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.            Thread [DDDDDDDDDD] - Setby_GGGGGGGGGG'.Thread [DDDDDDDDDD] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.Thread [IIIIIIIIII] complete. Our 'threadlocal' string is now - Setby_GGGGGGGGGG'.============== all threads complete.

我没有包括创建、启动和加入线程的类 - 如果觉得这有帮助,我很乐意编辑以添加它。

最佳答案

拥有单独的实例没有帮助,因为所有实例都具有相同的静态字段。可变静态是邪恶的。

如果您真的不能更改类,您可能只想使用锁,这样每个客户端都可以一次使用一个静态字段。如果你想要静态字段的不同实例,那么你可能需要使用类加载器(另一个明显的解决方案是重写字节码,这更不愉快)。

关于java - 将 ThreadLocal 与包含静态成员的现有类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1319825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com