• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Is static inner class thread safe?

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We know static variables are not thread safe. How about static inner class?
This is an example:
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, by definition that is not an inner class. It is a static nested class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the responses!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, good point!

I'm moving this to our Threads and Synchronization forum...
 
reply
    Bookmark Topic Watch Topic
  • New Topic