wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help me understand synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help me understand synchronized" Watch "Help me understand synchronized" New topic
Author

Help me understand synchronized

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi!

I want some clarification regarding synchronized() and wait()/notify()

My question, will two threads enter the synchronized block over an object.


Both the classes extend Thread threads share the same monitor obj;
My question is "No two threads can enter the synchronized block over a monitor concurrently", So while a thread is executing the run method of the
class2 how will another would enter to wait()? As I think i wont go inside the sync block. I am missing a vital part as I guess. Please make me understand the whole concept.


Thanks in advance,
cmbhatt


cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

If "Obj" is referring to same Object in both "class1" and "class2"

case 1) if thread of class1 first got "obj" lock:
upon calling wait() it will release the lock on "obj" and enters
wait state.

case 2) if thread of class2 first got "obj" lock:
Until unless notify called, it won't release the lock.
till that time no thread class1 can enter wait state.


Hope its clear.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
hi Srini,

Thanks for prompt reply. But you answer is correct if you reverse the cases, as it seems to me. Please correct me.


Thanks,
cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
I didn't get you. Both cases are independent.
[ March 27, 2007: Message edited by: Srinivasan thoyyeti ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


If Thread of class1 acquires the lock it will release the lock;
Meanwhile if the thread of class2 acquires the lock it would do the calculation and notify the monitor. Now the thread of the class one can procede.

What is the monitor is notified first and after then the thread of class1 enters the eait state, it would go on wating and there wont be anybody to make him inform that object (monitor) has been notified.

How the situation is handled! What you said :

case 1) if thread of class1 first got "obj" lock:
upon calling wait() it will release the lock on "obj" and enters
wait state.

case 2) if thread of class2 first got "obj" lock:
Until unless notify called, it won't release the lock.
till that time no thread class1 can enter wait state.

case 2) seems dubious to me, why wont it release the lock, it does the job and comes out, releases the lock. Why would it require, the monitor to be nonified? it is not waiting. (it has not called wait())


Yeah! Srini I think now you got me!


Srini:
"Both cases are independent"


Even if both cases are independent, why can't other threads enter the sync block, if the thread of class2 has done its job.


Thanks for prompt consideration,
cmbhatt
[ March 27, 2007: Message edited by: Chandra Bhatt ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,
Even if both cases are independent, why can't other threads enter the sync block, if the thread of class2 has done its job.


If class2 thread job is over then, any class1 thread which is waiting to obtain lock on "obj" will get it.


If you still have some very specific in mind let me that.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Srini,

Actually I created this thread, with a maddy confusion, but going parallel
,discussion went broad.


All the threads of class1 will enter the sync block acquiring the lock and leave the lock and wait. If meanwhile thread of class2 acquires the lock, it would perform operation and come out after notifying the monitor "obj", consequently one of the waiting thread of class1 will come into runnable state from the waiting state.

Thanks for your cooperation & please correct me if anything goes wrong what I wrote above.

Thanks again,
cmbhatt

[ March 27, 2007: Message edited by: Chandra Bhatt ]
[ March 27, 2007: Message edited by: Chandra Bhatt ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

You are correct.
your posts are good.

Keep it going..
Have a nice day.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

My question is "No two threads can enter the synchronized block over a monitor concurrently", So while a thread is executing the run method of the class2 how will another would enter to wait()? As I think i wont go inside the sync block. I am missing a vital part as I guess. Please make me understand the whole concept.


If class2 has the lock before class1 is able to acquire it, class1 will block at the point before it enters the sync block. The notification is then sent, and tossed, because no thread is in a wait state.

This is why you should always confirm that the thread needs to wait() before waiting. There should always be some sort of indicator like a flag. If there was one, then class1 could check that it is already in the state it requires, and not need to wait.

Always check to see if you should wait() before calling wait().

Also, a waiting thread should recheck the indicator after waking up from wait to reconfirm. The reason is, it is possible for the notification to be sent, but before the first thread can wake up, another thread calls class1, checks the indicator and grabs the resouce. The original thread that called class1, then wakes up after recieving notification.

Always recheck to see if you should call wait() again after calling wait().

Code should look something like this:



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Henry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me understand synchronized