静态内部类在另一个java类中线程安全吗?
为了收集较小的帮助器实用程序类,我创建了一个通用类 MyUtils :
// MyUtils.java
public final class MyUtils
{
public static class Helper1 {};
public static class Helper2 {};
//...
}
这个来自 MyUtils 内部的帮助器类将在包的其他文件中使用:
// MyClass1.java
public class MyClass1
{
private MyUtils.Helper1 help1 = new MyUtils.Helper1();
public void method ()
{
private MyUtils.Helper2 help2 = new MyUtils.Helper2();
}
}
让它们可访问,我已将它们设置为 MyUtils
内的static
(它自己没有任何数据/函数成员)。在创建 MyUtils
之前,我的代码是线程安全的。
我担心的是,通过使这些内部
类静态
,当它们的多个实例存在于文件中时,它们是否会保持线程安全?或者我是否因为将它们设为静态而忽略了它们的任何不良含义?
编辑:我没有触及 helper
类中的任何共享变量。我唯一关心的是静态类的实例是否是线程安全的(因为它们是静态的)。
For collection of smaller helper utility classes, I have created a general class MyUtils
:
// MyUtils.java
public final class MyUtils
{
public static class Helper1 {};
public static class Helper2 {};
//...
}
This helper classes from inside MyUtils
will be used in the other files of the package:
// MyClass1.java
public class MyClass1
{
private MyUtils.Helper1 help1 = new MyUtils.Helper1();
public void method ()
{
private MyUtils.Helper2 help2 = new MyUtils.Helper2();
}
}
To let them accessible, I have made them static
inside MyUtils
(which doesn't have any data/function member of its own). My code is thread safe before creating MyUtils
.
My worry is, by making these inner
classes static
will they remain thread safe, when their multiple instances will exist across the files ? Or is their any bad implication am I missing due to making them static
?
Edit: I am not touching any shared variable inside the helper
classes. My only concern was that will the instance of the static
classes be thread safe (since they are static
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经得到答案了。将
MyUtils
设为interface
是更简洁的设计,因为我可以摆脱helper
类中的static
identifienrI have got the answer. Making
MyUtils
aninterface
is more cleaner design, as I can get away with thestatic
identifienr from thehelper
classes如果您问从:
...到:
是否有任何不好的暗示,那么不,没有。在这种情况下,
static
关键字只是将嵌套内部类“提升”为顶级类,以便您可以实例化它,而无需MyUtils
的封闭实例。这是关于该主题的一篇还不错的文章:http://www.javaworld.com /javaworld/javaqa/1999-08/01-qa-static2.html
本质上,在嵌套内部类上执行
public static class X
与执行public 相同班级X
在标准顶级类中。If you're asking whether these is any bad implication of going from:
...to:
Then no, there is not. The
static
keyword in this case is just "promoting" the nested inner class to a top-level class, so that you can instantiate it without needing an enclosing instance ofMyUtils
. Here is a passable article on the subject:http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html
In essence, doing
public static class X
on a nested inner-class is the same as doingpublic class X
in a standard top-level class.“类”本身是线程安全的还是非线程安全的没有任何意义。因此,它是否是静态的并不重要。
当有人提到一个类是线程安全的或非线程安全的时,他们实际上是指该类提供的功能是线程安全的还是非线程安全的。因此,真正产生差异的是内部类本身所做的事情。
方法本身并不存在任何使它们不安全可重入的因素。当您开始访问共享变量等时,就会出现问题。因此,例如,方法访问的类的成员需要适当同步。但是,如果这些方法不存储任何状态等,那么就没有什么可以阻止您跨多个线程使用它们。
希望有帮助。
There is no meaning to a "class" itself being thread-safe or not thread safe. Therefore, whether or not it is static is irrelevant.
When someone refers to a class being thread-safe or not thread-safe, they really mean that the functionalities provided by that class are thread-safe or not. Accordingly, it's what the inner classes do themselves that actually makes the difference.
There's nothing inherent about methods that make them unsafe to be reentrant. Problems arise when you start accessing shared variables, etc. So, for example, a member of the class accessed by the methods needs to be synchronized appropriately. But if the methods don't store any state, etc., then there's nothing stopping you from using them across multiple threads.
Hope that helps.
您需要保护对
help1
的访问,因为这是一个实例级(共享)变量。如果您不允许
help2
跳过该方法,那么它是安全的。静态类和由它创建的实例没有什么特别的。
线程安全的相同规则适用于静态类的实例,也适用于正常情况。
You will need to guard the access to
help1
since this is an instance level (shared) variable.While
help2
is safe if you dont allow it to skip the method.There is nothing special about the static classes and instance created out of it.
Same rules of thread safety applies to instances of static classes also which applies to normal cases.
静态方法和内部类无法访问其动态对应部分的变量,因此无法在其父类的实例上使用监视器/同步。当然,这并不意味着声明它们和使用它们本质上是非线程安全的。只是,如果您需要在父类的实例上同步任何这些静态方法,那么您需要确保在输入它们之前同步/锁定它们,否则您必须显式传递引用到它们的父实例。
static methods and inner classes don't have any access to the variables of their dynamic counter part, and consequently can't use monitors/synchronize on an instance of their parent class. Of course this doesn't mean that declaring them and using them is inherently non-thread safe. It's just that if you need to synchronize any of those static methods on an instance of the parent class, then you need to be sure that you synchronize/lock before entering them or else you must explicitly pass a reference to a parent instance into them.