my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about Threads notify! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about Threads notify!" Watch "about Threads notify!" New topic

about Threads notify!

Paul J

Joined: Feb 06, 2002
Posts: 2
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();
System.out.println("Waiting for b to complete");
}catch(InterruptedException e){
System.out.println("InterruptedException open");
System.out.println("Total is :";
class ThreadB extends Thread
int total;
public void run(){
for(int i=0;i<100;i++){
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 ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
I think compiler does not force you to call notify().
As notify() can be called on same object from anywhere.
================ FILE =======

=================== FILE ===

As you can see there are TWO different file but notify() can be called on the object of ThreadB.
So compiler does not force you to put notify() in same file or in same class.

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Paul J

Joined: Feb 06, 2002
Posts: 2
Thanks your response.I think you are right.But I write some test code as follows,It can run correctly without "notify()" in any Class.

We can see,there is not notify in ThreadB.It can still run ,which isn't in accord with the ideal--"Must notify() when use wait(). ".
Execute my english.
[ March 18, 2002: Message edited by: Paul J ]
I agree. Here's the link:
subject: about Threads notify!
It's not a secret anymore!