• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Thread

 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Here is a question from a mock exam,
What is the name of the method that threads can use to pause their execution until signalled to continue by another thread?
Fill in the name of the method.
Answer: wait()
I was wondering if join() is a better answer. Because a thread waits until it is signalled by the object that it is modified (or whatever.. by using the notifyall method) and not by another thread.
But if you say join a method waits for its child thread to be completed. Don't you feel join is a better answer.
Please explain.
Thx in advance.
bye.
sk
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the key is "signalled by another thread" which is what the wait()/notify()/notifyAll() set of methods is intended for.
join() doesn't give the option of the threads operating concurrently - as you point out the thread that you've "joined" to must complete before the joining thread can continue.

With wait()/notify()/notifyAll() you're able to have communication between the threads and can setup, for example, an ongoing producer-consumer
relationship...
Hope that helps.
 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx Rich,
Can you please throw more light on the wait(), notify() and notifyAll() methods. I am confused if these methods are to be called by the object or the Thread. How do u really use them. I understand that wait will cause a thread to wait until the object on which it is waiting indicates that it is modified.(Is notify called by thread or the object).
Thanks in advance.
bye
sk
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sai kumar:
Thx Rich,
Can you please throw more light on the wait(), notify() and notifyAll() methods. I am confused if these methods are to be called by the object or the Thread. How do u really use them. I understand that wait will cause a thread to wait until the object on which it is waiting indicates that it is modified.(Is notify called by thread or the object).
Thanks in advance.
bye
sk

They need to be called within a synchronized context (i.e. either a synchronized method or a synchronzed block of code) and the caller needs to have acquired the lock on the object that the wait()/notify()/notifyAll() method is being called on....
If you think about this, it really doesn't make sense to use these UNLESS you're working with multiple threads (otherwise you could just write your program to process the required steps sequentially and avoid all the "messiness")...presumably you're using the threads to improve performance (or improve "perceived" performance by making your application more responsive to the user and not making the user wait while you're doing other processing). You wind up calling them on some object within some number of threads....but remember that a Thread is an Object, so there's no reason why you can't call the methods on a Thread object, if your application is designed that way....
The following psuedo-code might help
 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx rich
bye
sk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic