• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread synchronized: Kathy Sierra book question

 
pranay hira
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
prashanth kumar
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic