GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Synchronised block question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronised block question?" Watch "Synchronised block question?" New topic
Author

Synchronised block question?

Mathew Chen
Greenhorn

Joined: Aug 01, 2005
Posts: 18
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is how I interpreted your description:



The answer is yes, two threads can call a() and b() at the same time.


[Jess in Action][AskingGoodQuestions]
Mathew Chen
Greenhorn

Joined: Aug 01, 2005
Posts: 18
Thanks a lot for your reply.

Mathew Chen
Mathew Chen
Greenhorn

Joined: Aug 01, 2005
Posts: 18
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

Joined: Aug 10, 2005
Posts: 47
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

Joined: Nov 22, 2008
Posts: 18944
Just be careful that u are referring to the object "o" but not to the object instance that is created from class "X" , here.
 
Don't get me started about those stupid light bulbs.
 
subject: Synchronised block question?