Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static inner class thread safe inside another java class?

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

like image 516
iammilind Avatar asked Jun 24 '11 04:06

iammilind


People also ask

Is static class thread-safe in Java?

A data type or static method is threadsafe if it behaves correctly when used from multiple threads, regardless of how those threads are executed, and without demanding additional coordination from the calling code.

Can inner classes be static in Java?

Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Is static inner class thread-safe?

The short answer is a singleton can be made thread-safe by instantiating the singleton class inside a static inner class or static initializer block.

Why is static inner class singleton thread-safe?

Thread Safe Singleton: A thread safe singleton is created so that singleton property is maintained even in multithreaded environment. To make a singleton class thread safe, getInstance() method is made synchronized so that multiple threads can't access it simultaneously.


2 Answers

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.

like image 44
Shirik Avatar answered Oct 09 '22 12:10

Shirik


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.

like image 123
aroth Avatar answered Oct 09 '22 12:10

aroth