• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we use thread object as a lock  RSS feed

 
Ponraj Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Finally i got the problem and solution !!
Thanks a lot.
Regards
Ponraj R
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!