Page 1 of 1
About Question enthuware.ocpjp.v8.2.1536 :
Posted: Wed Mar 09, 2016 5:33 pm
by digitalillusion
Hi,
would it be wrong to use this as monitor for synchronization (including invocation of wait and notifyAll) instead of data?
Thanks
Re: About Question enthuware.ocpjp.v8.2.1536 :
Posted: Wed Mar 09, 2016 11:33 pm
by admin
Yes, that would be wrong. You cannot use this to synchronize in this situation because this in Producer class will refer to the Produce instance and this in Consumer class will refer to the Consumer instance. For two threads (Producer and Consumer) to access the same shared resource (which is data in this situation), they need to synchronize on the same lock. That is why synchronizing on "data" is correct here.
HTH,
Paul.