Hi everbody,
I am reading a book about Thread.It tell me :
"The method wait() and notify() are instance methods of Object.In the same way that every objet has a lock,every object can have a list of threads that are waiting for a signal from the object.A thread gets on this list by executing the wait() method of the target object.From that moment,it doesn't execute any further instructions until the notify()method of the target object is called."
I write a example:
------------------------------
public class test
{
public static void main(String [] args)
{
ThreadB b=new ThreadB();
b.start();
synchronized(b){
try{
System.out.println("Waiting for b to complete");
b.wait();
}catch(InterruptedException e){
System.out.println("InterruptedException open");
}
}
System.out.println("Total is :"+b.total);
}
};
class ThreadB extends Thread
{
int total;
public void run(){
synchronized(this){
for(int i=0;i<100;i++){
total+=i;
}
notify();
}
}
};
-----------------------
It can be compiled and run correctly without question.But in fact,if I delete the "notify()" in method run() of class ThreadB,it can still be compiled and run correctly.Why?
[ March 18, 2002: Message edited by: Paul J ]
I am reading a book about Thread.It tell me :
"The method wait() and notify() are instance methods of Object.In the same way that every objet has a lock,every object can have a list of threads that are waiting for a signal from the object.A thread gets on this list by executing the wait() method of the target object.From that moment,it doesn't execute any further instructions until the notify()method of the target object is called."
I write a example:
------------------------------
public class test
{
public static void main(String [] args)
{
ThreadB b=new ThreadB();
b.start();
synchronized(b){
try{
System.out.println("Waiting for b to complete");
b.wait();
}catch(InterruptedException e){
System.out.println("InterruptedException open");
}
}
System.out.println("Total is :"+b.total);
}
};
class ThreadB extends Thread
{
int total;
public void run(){
synchronized(this){
for(int i=0;i<100;i++){
total+=i;
}
notify();
}
}
};
-----------------------
It can be compiled and run correctly without question.But in fact,if I delete the "notify()" in method run() of class ThreadB,it can still be compiled and run correctly.Why?
[ March 18, 2002: Message edited by: Paul J ]