Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me for solving this  RSS feed

 
jolly Shah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to multithread programming.I have some doubt.

If one class C has 2 static synchronised method f1 and f2.
Class C has two instances O1 and O2.
Two threads are created t1 and t2 and if we call
T1(O1,f1) and T2(O2,f2) then both the thread will block each other or not.
Please give the explanation for your answer also.
I will be really appreciate any explanation.

Regards,
Jolly
 
Andreas Schaefer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, only one will be blocked temporarly and the reason is because static methods are locking on the Class (C.class). Actually it is completely irrelevant that you have two instances because T1(C,f1) and T2(C,f2) would lead to the same problem.
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As Andreas said that is correct but let me Rephrase the concept of synchronization for static and no-static methods.
As we know static methods defination belongs to class not to object. So whenever you call any static method JAVA creates Class class object for the class in which static method is defined.And you invoke that method on Class's object.
Fact about synchronization is that,Java blocks all synchronized methods, moment someone call any synchronized method on any class.
e.g Say class 'A' contains 6 method {m1,m2...m6}which are declared as synchronized, So if thread T1 invoke m1 on class 'A' object then remaining 5 method will blocked by JVM.
So in your case one method will be blocked.
Thanks
Sunil Dixit
 
jolly Shah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for answering my question.
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil

Are you saying that all syncronised methods are blocked... even of they are non-static?

Shilpi
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shilpi,
Yea Thats right.....
thanks
Sunil
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil, you seem to be opening up a number of issues which were not part of the original question, and providing incorrect information about them. Static synchronized methods and nonstatic synchronized methods do not block each other at all. They are completely independent, as they synchronize on different monitors. Static methods synchronize on the Class object for the class, and nonstatic methods synchronize on the individual instances used to call the methods.

As for the original question, Andreas Schaefer already seems to have answered it correctly. No one had asked about nonstatic metnods.
[ May 17, 2006: Message edited by: Jim Yingst ]
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,
Thanks for your advice, but i don't think so neither i am providing wrong information nor opening up a number of issues which were not part of the original question.
I never told that static synchronized method will block non static synchronized method.
I told him that once the thred tries to execute any synchronized method (static/non-static),than first it acquires the lock on that object and the moment lock is acquired on that object no else thread can execute any synchronized method on that object,while that thread can execute any synchronized method coz that thread got the object lock.
I just letting the people know how static and non-static synchronized method works.
Thanks
Sunil Dixit
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim's point is that your explanation is full of mis-statements -- perhaps just because you're not choosing your words very carefully. For example, Shipi says "Are you saying that all syncronised methods are blocked... even of they are non-static?" and you reply "Yes, that's right." But you don't explain what you mean, or what your understanding of the question actually is.

The truth of the matter is that synchronized static methods on a single class will block calls to the synchronized static methods of that same class only, while synchronized instance methods of a specific instance will block calls to synchronized instance methods of that same instance only. Your statements strongly suggest that you believe otherwise -- but as I said, perhaps this is just due to imprecise use of language.
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me rephrase EFH's reply in case of non-static synchronized methods, please let me know if I am wrong :

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is incorrect. The calls to method1() and method2() are being made from the same thread, sequentially. This means method1() will complete, releasing the sync lock on obj1OfA, before the method2() call is even attempted.
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i forgot to mention the assumption that each of the method calls is made in seperate threads....

did this just to avoid writing more sample code

now, based on this assumption, is what I said OK?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ashish]: now, based on this assumption, is what I said OK?

Um, probably. If we introduce other unstated assumptions. In general it's hard to say whether method1() or method2() would execute first, if they're called from separate threads. But if method1() starts executing before method2(), and if it takes long enough that it's still executing when method2() is called, then your statements seem correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!