posted 14 years ago
I am sorry, but may I get your attention to the below point?
Observe that the method ‘workWithLocks()’ is actually acquiring the lock on the objects (lock1) and (lock2) and NOT on ‘this’.
The variables i1, i2, j1, j2, k1, k2 are NOT the members of Object lock1 or Object lock2.
Acquiring the lock on Object lock1 or Object lock2 CANNOT prevent threads from accessing variables i1, i2, j1, j2, k1, k2 concurrently.
Further, declaring variables i1, i2, j1, j2, k1, k2 would NOT help either as volatile will ONLY make sure that all the threads will always receive the latest value of these variables.
Am I right?
Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%