| Author |
Can we use thread object as a lock
|
Ponraj Ram
Greenhorn
Joined: Sep 15, 2005
Posts: 12
|
|
Hi all, I have used a thread object as a lock for Synchronization context like this :- case 1: case 2: Whats the difference between the above two case. And case 1 is behaving differently when i started MyThread from case2. For Example The Code inside sync context is not waiting, and no Exceptions. But, the code below is waiting.. And the run method of MyThread is:- Thanks Ponraj Ramasubbu.
|
 |
Ponraj Ram
Greenhorn
Joined: Sep 15, 2005
Posts: 12
|
|
Hi all, One modification Whats the difference between the above two case. And case 1 is behaving differently when i started MyThread from case2. to Whats the difference between the above two case. And case 1 is behaving differently from case2, when i started MyThread.
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
I would have thought that both of your examples would wait in wait(), because you have no notify() calls anywhere. Using a Thread object as a monitor for synchronize is legal, but not advisable. Who knows what other system things might be synchronising on the Thread object?! Use an object over which you have full control.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Using wait() causes the current thread of execution to wait. When you call thrd.wait() you are causing your current thread to wait on the Object thrd rather than causing the Thread thrd to wait. In your case thrd is a completely different Thread that is executing.
|
 |
Jon Egan
Ranch Hand
Joined: Mar 24, 2004
Posts: 83
|
|
Ponraj, This sounded all to familiar, I had to go back and find the post where I ran into this before.... Someone else was calling wait() on a Thread object like you are, and after I dug and dug, someone responded a Thread object calls notifyAll() when it dies. Hope this helps, -- Jon
|
 |
Sridhar Subramaniam
Greenhorn
Joined: May 22, 2005
Posts: 4
|
|
Yes you don't see the difference if you call the run method just like any other instance method. Whenever the thread completes its run method, it make sense to notify all the objects that are waiting for this thread.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Sridhar Subramaniam: Whenever the thread completes its run method, it make sense to notify all the objects that are waiting for this thread.
In fact this is how the join() method is implemented, if I remember correctly: it waits on the thread object until it died.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
|
I'm not understanding where this discussion is heading. Unless I missed something it appears that the code that "doesn't stop" is in MyThread's run method which they create and start. For some reason they seem to think that code outside of that being executed in a completely different thread calling wait() on MyThread is going to stop that MyThread's execution.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Ken, I think what you are missing is that thrd.wait(); waits because noone is calling thrd.notifyAll() to wake it up - unless you call thrd.start() beforehand, which will do exactly this once the run method has finished.
|
 |
Will Bramble
Greenhorn
Joined: Jan 21, 2006
Posts: 10
|
|
|
Hm I thought Ken had it: you can't pause a running thread using a call to wait() from a different thread..? The sync block does own the lock though, which is why there's no exception.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Will Bramble: Hm I thought Ken had it: you can't pause a running thread using a call to wait() from a different thread..?
I don't think anyone is trying to. It's the *main thread* that pauses - and the question was why starting the thread changes how long it pauses.
|
 |
Will Bramble
Greenhorn
Joined: Jan 21, 2006
Posts: 10
|
|
Ah OK I think I'm with it now. I think the original question is not just why starting the thread changes how long it pauses, but also how would this change if we instead used another object (a dummy Object instead of 'thrd') to synchronize and wait on. Eg, the main thread in the following code -would- wait, whereas the one using 'thrd' as the sync object doesn't: So as already mentioned there's the hidden call to thrd.notifyAll after the thread ends.. and if you instead chose to synchronize on a dummy object in the main thread as shown above, this call would have no effect on your waiting block.
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Originally posted by Ilja Preuss: I don't think anyone is trying to. It's the *main thread* that pauses - and the question was why starting the thread changes how long it pauses.
I was under the impression that the OP thought it wasn't pausing because the code in MyThread was still executing. Of course, since they're not calling wait() from MyThread but rather on MyThread I thought they misunderstood wait(). At this point I don't even know what the question is since it's not clear where exactly this code is being executed from anyway.
|
 |
Ponraj Ram
Greenhorn
Joined: Sep 15, 2005
Posts: 12
|
|
Hi All, Finally i got the problem and solution !! Thanks a lot. Regards Ponraj R
|
 |
 |
|
|
subject: Can we use thread object as a lock
|
|
|