Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

an parent object's lock  RSS feed

 
zhang feng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
every object has a lock,but consided the "abstract" class,that isn't instance,if it has a synchronized method ,i invoked the method from the driver class,i will receive the which locks?
the following class indicate my means:

i invoke the BaseClass f() and g(),here,i will have a lock,is Myclass's object my1,my2?i think that have two locks,but i don't know that the my1 lock and my2 lock associate the BaseClass?thus,if i reveive the BaseClass's locks?why ?
thanks your help !
[ October 09, 2003: Message edited by: zhang feng ]
[ October 09, 2003: Message edited by: zhang feng ]
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're asking.
If you have 2 objects (as distinct from classes) then each will have its own lock. Also, you are implying use of method level locks which are distinct from a lock associated on an object.
Synchronized( my1 ){...} will obtain a lock on one of your objects and no other Thread will be able to obtain a lock on that object.
Calling my1.f() and my1.g() concurrently from different Threads can be done as they are distinct locks.
Calling my1.f() concurrently from different Threads will be prevented as the method is Synchronized.
Does this help?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, you are implying use of method level locks which are distinct from a lock associated on an object.

But Java doesn't have a concept of "method-level locks." You could use a synchronized block and a specific object to lock a single method, but I don't think that's what he's asking.
zhang feng --
  • Every physical object has one, single lock built into it.
  • An instance of a Java class that extends another Java class is not represented in memory by an object of the subclass and an object of the base class connected by a pointer, or anything like that. The subclass instance is just one, single object, which includes all the data and methods of both the subclass and the base class.
  • Therefore, if there's a synchronized method in a base class, and a synchronized method in a subclass, and I've got an instance of the subclass, then both of these methods will use the same lock when called on that instance.

  • Does this answer your question?
     
    Jeremy Thornton
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ignore my nonsense about method level locks.
    Obviously away with the fairies.
    There is a lock per object that you instantiate.
    Subclass instances share static members (including those in the superclass)but this doesn't apply to regular (non-static) members.
    All objects are instantiated from classes derived from Object - your comments would imply that there is only a single lock available to java.
     
    zhang feng
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ernest Friedman-Hill :
    you tell me what I think , your means the sub-class include the all the parent class data(unprivate) and point to super-class method pointer.Althougth the sub-class extends the super class ,sub-class invoke the super-class's(not interface and abstract class) synchronized method,only received the sub-class lock,and not super-class.if the up eg,the BaseClass is not abstract,at the comment //1 ,only one myclass1 object,the lock is myclass1 object's.
    i right ?
    In the book Practice JAVA ,the author introduce the two synchronized way,used to the method and block.it's not say a method lock,only the object has lock.
    used two the ways ,only the efficience is not same , and synchronized method will aware the higher efficience.
     
    Jeremy Thornton
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Still not entirely sure what you're asking.
    If we consider my1.f() as calling the BaseClass method which could be considered to look something like:

    We can see that calling m1.f() and my2.g() will be synchronizing on two different this objects (as my1 and my2 are different objects).
    This might be completely unrelated to what you're *actually* asking :-( Perhaps rephrasing your question would help.
    Jeremy.
    [ October 13, 2003: Message edited by: Jeremy Thornton ]
     
    Ernest Friedman-Hill
    author and iconoclast
    Sheriff
    Posts: 24215
    37
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand your first paragraph and it sounds like you understand what I said, and you're correct.
    But this part

    In the book Practice JAVA ,the author introduce the two synchronized way,used to the method and block.it's not say a method lock,only the object has lock.
    used two the ways ,only the efficience is not same , and synchronized method will aware the higher efficience.

    I have no idea what you're trying to say.
     
    zhang feng
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ou,sorry,used to Practice JAVA only indicated my idea that is not method lock.I think that it's two synchronized form,and other nothing.
    I beg your pardon.Perheps my expression is not very clearly,my english is not very well.
    thanks your help .
    [ October 15, 2003: Message edited by: zhang feng ]
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Moving this to the Threads and Synchronization forum...
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!