Hello everyone,


Suppose I have a method which is a synchronized method (for example, method Foo in the following code block), and in this synchronized method another method which is not synchronized is invoked (for example, method Goo in the following code block). I am wondering when executing in Goo from Foo, whether we still have the synchronized feature (i.e. obtain the lock of "this" object).

synchronized public Foo()
{
    Goo();
}

private Goo()
{
}

Thanks in advance,
George

yes you do. Your lock doesn't get released until after you leave the synchronised code (and sometimes not even then, but those are abnormal situations).
As you are calling your method from within synchronised code your lock gets retained.

Thanks jwenting,

yes you do. Your lock doesn't get released until after you leave the synchronised code (and sometimes not even then, but those are abnormal situations).
As you are calling your method from within synchronised code your lock gets retained.

Your reply is very helpful. I am wondering what are abnormal situations?


regards,
George

there is a possibility of an InterruptedException (depending on where it was raised etc.) causing a lock to break.

I'd have to look up the exact scenarios, they're rather obscure but I should know them (still need to solidly revise that part of my exam prep book ;) ).

Thanks jwenting,

there is a possibility of an InterruptedException (depending on where it was raised etc.) causing a lock to break.

I'd have to look up the exact scenarios, they're rather obscure but I should know them (still need to solidly revise that part of my exam prep book ;) ).

If you have found the sample source codes, please post them here. I am very interested in it.


regards,
George

I'm a bookworm.
Get yourself this: http://www.amazon.com/exec/obidos/ASIN/0072226846
Even if you don't plan the certification it's a great resource on all the nitty gritty details of the language which you never thought you wanted to know ;)

Thanks jwenting,

I'm a bookworm.
Get yourself this: http://www.amazon.com/exec/obidos/ASIN/0072226846
Even if you don't plan the certification it's a great resource on all the nitty gritty details of the language which you never thought you wanted to know ;)

It seems a good book. I am wondering whether I can find an online version which can save my money. :cheesy:


regards,
George

nowhere. It's well worth the money. Once you're through with it it makes a fine paperweight, or you can use it to replace that pack of paper you're using to make your screen stand taller :)

Oh jwenting, I think you are more valuable than the book. You are an experienced and warm-hearted developer. :cheesy:

nowhere. It's well worth the money. Once you're through with it it makes a fine paperweight, or you can use it to replace that pack of paper you're using to make your screen stand taller :)

regards,
George

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.