• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread interaction

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic