• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is static inner class thread safe?  RSS feed

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know static variables are not thread safe. How about static inner class?
This is an example:
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question isn't really specific to the Servlet API so I am going to move it to our Java in General (Intermediate) forum.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread safety is an issue that gains relevance when you realize that more than one thread could be concurrently performing read/write operations over a particular variable, either static or non-static.

Therefore, it does not matter if your class is an static inner class or not. The questions is, is there any possibility that more than one thread is reading/writing its members concurrently?

If the answer is yes, then your class may not be thread safe, and any intend to modify any of its members should be properly synchronized.
[ July 25, 2006: Message edited by: Edwin Dalorzo ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, by definition that is not an inner class. It is a static nested class.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, at runtime there are no inner/nested classes - the JVM does only know about top level classes. All this inner/nested classes stuff is just syntactic sugar that gets translated by the compiler to top level classes.

Consequently, whether a class is a inner/nested class or not has no impact at all on thread safety.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the responses!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we're talking about static nested classes (not inner), then I agree there's no difference in thread safety; they're just like top-level classes. If we're talking about inner classes (not static) then I would say there is one extra thing to worry about. An inner class usually has access to instance fields of an enclosing class instance. (Unless the inner class was declared in a static context.) This means that when you're figuring out what methods to synchronize in order to protect mutable data from concurrent access, you need to consider the enclosing class instance fields as well. It's certainly still possible to use inner classes in a thread-safe manner - I'm just saying that inner classes create one more possible complication for you, which you need to watch out for.
[ July 25, 2006: Message edited by: Jim Yingst ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, good point!

I'm moving this to our Threads and Synchronization forum...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!