• 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

Thread synchronized: Kathy Sierra book question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

Iam trying to understand the following question of the SCJP 5.0 book of Kathy Sierra and Bert Bates:


The answer to this question is that you can add the following implementation of the run method to make sure (kind of a guarantee) that it will produce the output XXYY or YYXX and no other combination of X and Y:

1. public void run() { synchronized(Letters.class) {write();}}
and
2. public void run() { synchronized(System.out) {write();}}

I understand the second answer but I dont understand the first answer. I thought that obtaining the lock of a class is different from obtaining a lock on an instance. And this program is using two instances of the Letter class.

Can someone please explain me why a synch on the Letters.class is also correct?

Kind regard,
Pranay
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> public void run() { synchronized(Letters.class) {write();}}
>>
>> new Letters("X").start();
>> new Letters("Y").start();

There is only one lock for a CLASS...So in the above code,though there are 2 object instances,they are taking a class level lock...So one instance cannot run until the other finishes...Also one more important think is that class level locks has got nothing to do with instance level locks..

Hope this helps
Prashanth
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The answer to this question is that you can add the following implementation of the run method to make sure (kind of a guarantee) that it will produce the output XXYY or YYXX and no other combination of X and Y:

1. public void run() { synchronized(Letters.class) {write();}}
and
2. public void run() { synchronized(System.out) {write();}}

I understand the second answer but I dont understand the first answer. I thought that obtaining the lock of a class is different from obtaining a lock on an instance. And this program is using two instances of the Letter class.

Can someone please explain me why a synch on the Letters.class is also correct?



There is really no such thing as obtaining the "lock of a class". What the JVM does is obtain a lock on the instance of the Class object for that class.

The "Letter.class" variable is one way to get the Class object for the Letter class. And there is only one instance of a Class object per class.

Henry
 
pranay hira
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thanks for your explanations. I think I got confused because of the static and non static methods.

A static synch method gets the lock of an instance of the MyClass.class. A non static synch method gets the lock of the instance of the object.

Therefore stats and non stats method can't annoy each other in threads.

Kind Regards,
Pranay
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic