Hi,
After going thru 4-5 different sources of study materials on
Thread ( RHE, KAM, maha's site, etc) Here are few of my comments
on threads - methods - in general ...
1. Every
Java object is provided with methods like wait(),
notify(), notifyAll() for thread synchronization and Thread safe design. Use
word synchronized for methods in class to make them accessed by one only thread at a time. A general layout for a synchronized method is like this
public synchronized void doSomeThing()
{
while (//test some instance variable )
{
try{
wait(); // ask all the threads entering this method to
// wait !!
catch (InterruptedException e)}{}
}
// Do some automic operations
// Change the object status
notify() // notify that object has chaged its status and
// one of the wating threads can ente the method
}
Let me know your views on this general layout !!
2. Methods like sleep(), yield() are not called in shared object
(class's ) code as object yeilding or sleeping does not make sense insted it the thread that sleeps or yields.. They are called in threads code.
To conclude :
Following methods are used in thread or runnable class
sleep(), yield()
start(), run(), interrupt()
Following methods are used in any shared object class :
like MailBox class
wait(), notify() and notifyAll()
Please bear with me my way of asking question was wrong and confusing ... I just want to clarify my understanding of threads..
BTW I am appearing exam on Feb 23rd ...
Shrinivas