File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread interaction 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 "Thread interaction" Watch "Thread interaction" New topic
Author

Thread interaction

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134

Taken from K&B(with some added system outs).
1. If you comment out notifyAll(), all Reader threads comes back with the results; I'd have thought all Reader threads would be stuck waiting to be notified.

2. If you have notify(), instead of notifyAll() I'd expect one of the 3 Reader threads come back with results; but again none of the threads seem to have been blocked.

Can I have some explanation please or am I talking some nonsense?

Thanks



[HENRY: Added code tags]
[ October 26, 2006: Message edited by: Henry Wong ]
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Hi

I think You are seeing the same results because Yo did not setName for any Thread

Try it again and then tell me, this time setName


SCJP 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


Taken from K&B(with some added system outs).
1. If you comment out notifyAll(), all Reader threads comes back with the results; I'd have thought all Reader threads would be stuck waiting to be notified.

2. If you have notify(), instead of notifyAll() I'd expect one of the 3 Reader threads come back with results; but again none of the threads seem to have been blocked.

Can I have some explanation please or am I talking some nonsense?


What you are seeing is a side affect of the implementation of the join() method. The join() method simply checks the thread's alive flag, and if it is, it will just wait() on the thread object.

On the other side, when a thread completes (finishes the run() method), it does some internal cleanup -- to shutdown and cleanup after the thread. One action that it does is to send a notifyAll() on the thread object. This is so that threads that are trying to join() with this thread, can wake up and continue.

Henry
[ October 26, 2006: Message edited by: Henry Wong ]

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

Joined: Dec 07, 2004
Posts: 134
One action that it does is to send a notifyAll() on the thread object


So there is no effect of putting notify() or notifyAll() in the code as ints going to be called internally anyway?
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Henry Wong:


What you are seeing is a side affect of the implementation of the join() method. The join() method simply checks the thread's alive flag, and if it is, it will just wait() on the thread object.

On the other side, when a thread completes (finishes the run() method), it does some internal cleanup -- to shutdown and cleanup after the thread. One action that it does is to send a notifyAll() on the thread object. This is so that threads that are trying to join() with this thread, can wake up and continue.

Henry

[ October 26, 2006: Message edited by: Henry Wong ]



you are confusing me Henry
then why do we need notify() and notifyAll()?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by James Quinton:

you are confusing me Henry
then why do we need notify() and notifyAll()?



James,

What I was describing was... the join() method waits on the thread object internally as part of its implementation. As such, if you wait on the thread object, you will interfere with each other.

How did you conclude that notify() and notifyAll() are useless? Just don't use wait(), notify(), and notifyAll() with thread objects for threads that are terminating -- use other objects instead.

Henry
[ October 27, 2006: Message edited by: Henry Wong ]
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Originally posted by Mohammed EL-Adawi:
Hi

I think You are seeing the same results because Yo did not setName for any Thread

Try it again and then tell me, this time setName


Sorry this was non sense,
I donot know what I was thinking about

Actually, even if you have notifyAll() one or more threads could stuck, for example all Reader threads will stuck waiting on calculator object if calculator thread runs first.

and if you comment notifyAll() out, you gurantee to stuck on wait

I don't think that I saw any join in the code !!!
 
wood burning stoves
 
subject: Thread interaction
 
Similar Threads
Misbehaving variable?
blocking and waiting
blocking and waiting
Difference between notify and notifyAll
wait(), notify, notifyAll() doubt