• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread interaction

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic