| Author |
Thread synchronized: Kathy Sierra book question
|
pranay hira
Greenhorn
Joined: Jun 26, 2006
Posts: 15
|
|
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
|
-------------<br />SCJA<br />SCJP 1.4/5.0
|
 |
prashanth kumar
Ranch Hand
Joined: Mar 22, 2004
Posts: 162
|
|
>> 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
|
SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
pranay hira
Greenhorn
Joined: Jun 26, 2006
Posts: 15
|
|
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
|
 |
 |
|
|
subject: Thread synchronized: Kathy Sierra book question
|
|
|