静态内部类在另一个java类中线程安全吗?

发布于 2024-11-17 02:25:43 字数 810 浏览 3 评论 0原文

为了收集较小的帮助器实用程序类,我创建了一个通用类 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 staticwill 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 技术交流群。

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

发布评论

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

评论(5

入怼 2024-11-24 02:25:44

我已经得到答案了。将 MyUtils 设为 interface 是更简洁的设计,因为我可以摆脱 helper 类中的 static identifienr

I have got the answer. Making MyUtils an interface is more cleaner design, as I can get away with the static identifienr from the helper classes

┈┾☆殇 2024-11-24 02:25:43

如果您问从:

public class Helper1 {}

...到:

public class MyUtils {
    public static class Helper1 {}
}

是否有任何不好的暗示,那么不,没有。在这种情况下,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:

public class Helper1 {}

...to:

public class MyUtils {
    public static class Helper1 {}
}

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 of MyUtils. 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 doing public class X in a standard top-level class.

無處可尋 2024-11-24 02:25:43

“类”本身是线程安全的还是非线程安全的没有任何意义。因此,它是否是静态的并不重要。

当有人提到一个类是线程安全的或非线程安全的时,他们实际上是指该类提供的功能是线程安全的还是非线程安全的。因此,真正产生差异的是内部类本身所做的事情。

方法本身并不存在任何使它们不安全可重入的因素。当您开始访问共享变量等时,就会出现问题。因此,例如,方法访问的类的成员需要适当同步。但是,如果这些方法不存储任何状态等,那么就没有什么可以阻止您跨多个线程使用它们。

希望有帮助。

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.

没有心的人 2024-11-24 02:25:43

您需要保护对 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.

在风中等你 2024-11-24 02:25:43

静态方法和内部类无法访问其动态对应部分的变量,因此无法在其父类的实例上使用监视器/同步。当然,这并不意味着声明它们和使用它们本质上是非线程安全的。只是,如果您需要在父类的实例上同步任何这些静态方法,那么您需要确保在输入它们之前同步/锁定它们,否则您必须显式传递引用到它们的父实例。

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.

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