wood burning stoves
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronised block question?" Watch "Synchronised block question?" New topic

Synchronised block question?

Mathew Chen

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

Joined: Jul 08, 2003
Posts: 24193

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

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

Mathew Chen
Mathew Chen

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?

Mathew Chen
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
Yes if:
void b() {
synchronized (o)
{ // MORE

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
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?
It's not a secret anymore!