This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 24183
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronised block question?