Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

synchronised blocks

Thread view: 
patrick - 02 Jun 2006 21:34 GMT
methodA and methodB can be called from  different threads.

Does methodB wait to enter its synchronised block until methodA has exited
its synchronized block.??

method A()
{

  synchronized(arrayList)
 {
   doSomethingOne(arrayList);
 }
}

methodB()
{

  synchronized(arrayList1)
 {
   doSomethingTwo(arrayList);
 }
}

Or is there a better way of doing this. I want methodB not to run if methodA
is already running for a specific object arrayList.

TIA
patrick
Patricia Shanahan - 02 Jun 2006 21:44 GMT
> methodA and methodB can be called from  different threads.
>
[quoted text clipped - 24 lines]
> TIA
> patrick

The synchronized blocks in the two methods will exclude each other if
arrayList and arrayList1 are pointers to the same object.

Patricia
patrick - 02 Jun 2006 21:53 GMT
sorry typing error.  arrayList1 should read arrayList.
The two methods use the same object.
patrick

>> methodA and methodB can be called from  different threads.
>>
[quoted text clipped - 29 lines]
>
> Patricia
Patricia Shanahan - 03 Jun 2006 13:34 GMT
> sorry typing error.  arrayList1 should read arrayList.
> The two methods use the same object.

This may be wasted explanation of something you already understand, but
the importance you seem to attach to the variable name troubles me.

For synchronization, wait, and notify, it is VERY important to keep
clear about the distinction between a reference variable and the object
it references.

You can have two distinct reference variables that refer to the same
object. For example:

class Demo{
  List x = new ArrayList();
  List y = x;
  void methodA(){
    synchronized(x){...}
  }
  void methodB(){
    synchronized(y){...}
  }
}

If those are the only assignments to x and y then the synchronized
blocks in methodA and methodB, running in the same Demo instance, would
exclude each other.

Now suppose I create two instances of Demo:

Demo d1 = new Demo();
Demo d2 = new Demo();

d1.methodA and d2.methodA could run at the same time in different
threads, because d1.x and d2.x refer to different objects.

Patricia
patrick - 02 Jun 2006 21:57 GMT
methodA and methodB can be called from  different threads.

Does methodB wait to enter its synchronised block until methodA has exited
its synchronized block.??

method A()
{

  synchronized(arrayList)
 {
   doSomethingOne(arrayList);
 }
}

methodB()
{

  synchronized(arrayList)
 {
   doSomethingTwo(arrayList);
 }
}

> methodA and methodB can be called from  different threads.
>
[quoted text clipped - 24 lines]
> TIA
> patrick
Chris Uppal - 03 Jun 2006 11:01 GMT
> Does methodB wait to enter its synchronised block until methodA has exited

As Patricia said, they will block each other if and only if the reference
variable "arrayList" points to the same object in both cases.

   -- chris
patrick - 03 Jun 2006 18:47 GMT
Thanks for the replies.

yes I am referencing the same object so I am satisfied that
from your explanations about the methods being mutually exclusive.

And am using that in my program.seems ok.
But is there a more efficient way of doing it using perhaps
using some of the new concurrency classes in JDK5.0 ??

patrick

> methodA and methodB can be called from  different threads.
>
[quoted text clipped - 24 lines]
> TIA
> patrick


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.