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