• Post Reply Bookmark Topic Watch Topic
  • New Topic

static reference and ClassLoader

 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Storch
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!