This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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
 
permaculture is largely about replacing oil with people. And one tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic