• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static reference and ClassLoader

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to get better understanding of how JVM loads classes.

If for example I instantiate class A in two separate threads that run concurrently. Lets say that class A creates static instance of Hashtable. Will JVM guarantee that both threads will see the same Hashtable or this implementation is broken?

I know that DCL for singleton can be fixed by making instance static:


So does it mean that JVM synchronizes class loaders and only loads one class even if two threads try to instantiate it concurrently(running on 2 CPUs).

Thank you.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ClassLoading mechanism is definately Thread safe (synchronized).

But be carefull, if you have more than one ClassLoader. You can have the same class loaded twice in different ClassLoaders with different static Object references.
A class is unique by its full name and its ClassLoader.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter.

I just found the answer in JVM specs:

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19075

chapter 2.17.5

It states that init of any class is synchronized.
That is propably why JVM takes long time to start
reply
    Bookmark Topic Watch Topic
  • New Topic