All:
Here's a portion of text from K&B
SCJP book, chapter 9 (Threads / Synchronization and Locks), page 739:
Threads calling non-static synchronized methods in the same class will only block each other if they're invoked using the same instance. That's because they each lock the this instance, and if they're called using two different instances, they get two locks, which do not interfere with each other.
And from page740-741:
For changeable data in a not-static field, you usually use a non-static method to access it. By synchronizing that method, you will ensure that any threads trying to run that method using the same instance will be prevented from simultaneous access. But a thread working with a different instance will not be affected, because it's acquiring a lock on the other instance......
This text is confusing because we just finished Exercise 9-2 (Synchronizing a Block of Code) where, it states:
...Within that block of code we will get the lock on an object, so that other threads cannot modify it while the block of code is executing. We will create three threads that will all attempt to manipulate the same object....
So I interpret the exercise to demonstrate that a lock exists to prevent multiple instances of a
thread to access the same blocked code simultaneously, which it does. Doesn't that contradict what was stated about a thread working with "different' instances? This is so confusing
. Can someone perhaps provide an example of what the text is stating on pages 739 - 741, as I have quoted here?
Thank you all for your time.
respectfully,
Gary