• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronised block question?

 
Mathew Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Lets say I have a class with two methods method a() and method b().Now a() has a synchronised block of code and it is being called by a thread.Is it now possible for a nother thread to access method b() while method A() is being called?I guess the answer is "Yes" because the enter Object is not locked.Not sure if I am right.

Thanks in advance.

Mathew Chen
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I interpreted your description:



The answer is yes, two threads can call a() and b() at the same time.
 
Mathew Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply.

Mathew Chen
 
Mathew Chen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But suppose b() were synchronized too then it could not be called by another thread when the synchronised block in method a() is locked.Am I right?

Thanks
Mathew Chen
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes if:
CODE]
void b() {
synchronized (o)
{ // MORE
}
}[[/CODE]

since they are both requesting the lock of object o. When you say "synchronized", you have to be specific on object lock, in this case, it's from o
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just be careful that u are referring to the object "o" but not to the object instance that is created from class "X" , here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic