• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Sychronization.

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

Can we write code to synchronize on a java class? If so, what is the difference between synchronizing on a class and an object?

I would greatly appreciate an illustration with an example.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can synchronize on a class. Something like this would do:



Synchronizing on a class is little different from synchronizing on an object. It's important to remember that every object has exactly 1 lock. Likewise, a class has exactly 1 lock. So, in the code I wrote above, there would be 3 locks readily available - one for each Test object and one for the Test class (of course, you could lock on any other class, as well).

Sometimes, locking on a class, as opposed to an object, can be convenient so that multiple threads can synchronize without sharing access to an object.

I hope that helps,
Corey
 
Pal Sudarshan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As always, thanks for an excellent post Corey.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is class level synchronization on the SCJP exam?

Curious,

JerryB
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jerry Bustamente:
Is class level synchronization on the SCJP exam?

Curious,

JerryB



I don't know for sure, but my guess would be that it is. It's really a pretty simple concept. Just remember that, like every object, each class has a lock that can be used for synchronization.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic