Hi Donovan,
When you mark all your public methods synchronized (the simplest approach, the one I followed) you are synchronizing on the this-object. When you want to use synchronized blocks, you can synchronize on the this-object or you can synchronize on another object. One important note is that the 2 threads should access the same object (so the same instance). That's one of the basics of synchronizing and thread interaction,
you should be familiar with it, because you had to learn the basics for the
scjp exam.
That's why marking all public methods of the Data class synchronized will work only if combined with the singleton design
pattern.
Let's assume you have following code:
a snippet from the lock-method:
a snippet from the unlock-method:
This code is just to explain how locking is supposed to be implemented when using wait/notify/notifyAll-mechanism. So you must have 1 object that will be accessed by many threads.
Kind regards,
Roel