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

时间:2022-01-31 22:49:31

For collection of smaller helper utility classes, I have created a general class MyUtils:

为了收集较小的辅助实用程序类,我创建了一个通用类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:

MyUtils中的这个帮助器类将用在包的其他文件中:

// 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.

为了让它们可访问,我在MyUtils中使它们成为静态的(它没有自己的任何数据/函数成员)。在创建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).

编辑:我没有触及帮助程序类中的任何共享变量。我唯一担心的是静态类的实例是否是线程安全的(因为它们是静态的)。

5 个解决方案

#1


15  

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:

然后不,没有。在这种情况下,static关键字只是将嵌套的内部类“提升”到*类,因此您可以实例化它而无需封闭的MyUtils实例。这是一篇关于这个主题的可通过的文章:

http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html

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.

实质上,在嵌套内部类上执行公共静态类X与在标准*类中执行公共类X相同。

#2


5  

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.

希望有所帮助。

#3


0  

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.

您需要保护对help1的访问权限,因为这是一个实例级别(共享)变量。虽然如果你不允许它跳过这个方法,help2是安全的。

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.

静态类和从中创建的实例没有什么特别之处。相同的线程安全规则也适用于静态类的实例,这些实例也适用于正常情况。

#4


0  

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.

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

#5


0  

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

我有答案。使MyUtils成为一个界面更简洁的设计,因为我可以从助手类中摒弃静态识别器

#1


15  

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:

然后不,没有。在这种情况下,static关键字只是将嵌套的内部类“提升”到*类,因此您可以实例化它而无需封闭的MyUtils实例。这是一篇关于这个主题的可通过的文章:

http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html

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.

实质上,在嵌套内部类上执行公共静态类X与在标准*类中执行公共类X相同。

#2


5  

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.

希望有所帮助。

#3


0  

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.

您需要保护对help1的访问权限,因为这是一个实例级别(共享)变量。虽然如果你不允许它跳过这个方法,help2是安全的。

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.

静态类和从中创建的实例没有什么特别之处。相同的线程安全规则也适用于静态类的实例,这些实例也适用于正常情况。

#4


0  

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.

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

#5


0  

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

我有答案。使MyUtils成为一个界面更简洁的设计,因为我可以从助手类中摒弃静态识别器