File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Knotted up Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Knotted up Threads" Watch "Knotted up Threads" New topic
Author

Knotted up Threads

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
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


G
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I see no contradictions. Can you be more specific where you perceive the problem?
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121

I think what I'm having a problem with is understanding what the text is trying to tell me. So let me try this. Please see my inserted questions in the following text taken from the book:


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
<< by "same instance" is the text referring to the same instance of a running thread? >>
will be prevented from simultaneous access. But a thread working with a different instance
<< different instance of what? a different instance of an object containing synchronized code? >>
will not be affected, because it's acquiring a lock on the other instance......


Thank you
Gary
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Gary Marshall wrote:by "same instance" is the text referring to the same instance of a running thread?


No. Non-static methods are part of an object instance right? You can only call them through a specific object. That object will be the lock for the synchronized block. If two different threads call a synchronized method of that particular object, one of them will block.

different instance of what? a different instance of an object containing synchronized code?


Yep. This would be the object I referred to in your above question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Knotted up Threads