• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Knotted up Threads

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see no contradictions. Can you be more specific where you perceive the problem?
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic