Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Bastet Massri   on Jan 22 In Java Category.

  
Question Answered By: Willie Gomez   on Jan 22

Let me spell this out for you. This is taken straight from the javadocs on
Sun's website under the Object class for the definition of the wait method:

"Causes current thread to wait until another thread invokes the notify() method
or the notifyAll() method  for this object. In other words, this method behaves
exactly as if it simply performs the call wait(0). "

Read the first sentence carefully. Now let's look at your program  again:

class ThreadTest extends Thread {
public synchronized void notifyTest(){
System.out.println("notify will be called...");
try{
Thread.sleep(10000);
notify();
System.out.println("notify has called...");
Thread.sleep(10000);
}catch(Exception ex){ex.printStackTrace();}
}
public synchronized void waitTest(){
try{
wait();
System.out.println("wait has called..");
}catch(Exception ex){
ex.printStackTrace();
}
}
public void run(){
notifyTest();
}
public static void main(String arg[]){
ThreadTest t=new ThreadTest();
t.start();
t.waitTest();
}
}

I modified this slightly to save space. There are two threads I see running
here. There's the main thread which is started by the JVM and the thread class
that you create. Your thread will never run  if you never call the start  method.
So when you call waitTest, the only thread you are telling to wait is the main
JVM thread. That thread will then wait indefinitely because there is no other
thread that can notify it to stop.

However, if you start your own thread, you will first enter a sleep. Here is
what is written in the javadoc for Thread.sleep method:

"Causes the currently executing thread to sleep (temporarily cease execution)
for the specified number of milliseconds. The thread does not lose ownership of
any monitors."

What's important here is that second sentence. The thread has ceased execution
but it has not let go of that synchronized method so that some other thread can
call it. And, btw, you should not be using synchronized here because it does
not make any sense. You only synchronize on code  that you want to protect from
having several threads using it all at the same time.

The next thing that happens is you call notify. But it's wasted because no
thread is waiting. Then you go straight to the next sleep. Once the thread
exits the synchronized method notifyTest, the main thread is given permission to
do operations on your thread. It was not allowed to call any methods on your
thread before because of the synchronized lock you have on the notifyTest
method. If you put a println in between the start method call and the waitTest
method call you'll see that the main thread still runs even after your thread
has started but is not allowed to continue because the thread has been locked.

It is not an argument whether or not a thread can be used in this way because it
can't do the things you're trying to make it do. A thread can not tell itself
to wait and then later magically call notify itself to wake itself up. Another
thread has to call notify on the same synchronized code that the waiting thread
is waiting at. If you can produce an example of a thread that can tell itself
to wait and then later notify itself to wake up, then please do. The IO example
you provided doesn't make any sense.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on A challenging question about Threads&Synchronization Or get search suggestion and latest updates.