This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Thread" Watch "Question on Thread" New topic
Author

Question on Thread

sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
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

Joined: Feb 18, 2004
Posts: 96
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.


Rich
SCJP 1.4
sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
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

Joined: Feb 18, 2004
Posts: 96
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

Joined: Mar 31, 2004
Posts: 72
thx rich
bye
sk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Thread
 
Similar Threads
How to wait until all threads finishes task?
sleep() method
join()
join() mock exam question
Thread class join() method