• 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:

questions

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a questions regarding the SCJP exam:

1.
Can a static method be synchronized? If so, what is being locked
since there is no this reference?

2.
I thought one object has only one lock. Is that
not true?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) Yes static methods can be syncronised.
they are synchronized on the class
eg-syncronized(<class name>

2.)Yes each object has only one lock
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anne Sullivan:
I have a questions regarding the SCJP exam:

1.
Can a static method be synchronized? If so, what is being locked
since there is no this reference?



From Kathy Sierra's and Bert Bate's SCJP book,


Static
methods can be synchronized. There is only one copy of the static data you�re trying
to protect, so you only need one lock per class to synchronize static methods�a lock
for the whole class. There is such a lock; every class loaded in Java has a corresponding
instance of java.lang.Class representing that class. It�s that java.lang.Class instance
whose lock is used to protect the static methods of the class (if they�re synchronized).



Hope this clears!
 
reply
    Bookmark Topic Watch Topic
  • New Topic