[4129] in java-interest

home help back first fref pref prev next nref lref last post

Threading my way through threads

daemon@ATHENA.MIT.EDU (Michael Slominski)
Mon Dec 11 21:10:20 1995

Date: Mon, 11 Dec 1995 16:41:11 +0800
From: michael@earthlight.com (Michael Slominski)
To: java-interest@java.sun.com

I'm playing around with threads I and though I've been able to do most
of the things I want, I have a couple of question about doing threads
with JAVA.

First, I do not fully understand the scope of some of the key thread
related methods of the Object class. I've written two classes that
extend the Thread class: one that waits (by calling wait()) for
notification (called "Waiter"), and one that sleeps for a while and
then sends out a notification message (called "Signaler"). With a test
class, I instantiate one object of each of these classes and start
them up.

class Waiter extends Thread { 
  public void run() { waitForIt();}
  synchronized void waitForIt(){
      while (true) 
	try {
	    System.out.println("Waiter: waitState: Waiting");
	    wait();
	    System.out.println("Waiter: waitState: Done waiting");
	} catch (InterruptedException e) {}
  }
}

  class Signaler extends Thread{
      Waiter w;
      public Signaler(Waiter wtr){w = wtr;}
      public void run(){
         wait_some_time
         signal();
      }
      void synchronized signal(){notifyAll();}
  }

Questions:

1) The notifyAll() call in my Signaler.signal() method is not received
by the wait() call in my Waiter.waitForIt() method (Both methods are
synchronized). The documentation says that notifyAll() "Notifies all
of the threads waiting for a condition to change" and "Threads that
want to wait for a condition to change before proceeding can call
wait()", so what is the scope here? Does notifyAll()/wait() only
extend to different threads of the same object? 

2) I can notify the Waiter object from the Signaler object if the
Signaler object has a reference to the Water object to work with, but
only if I explicitly synchronize the reference to the Waiter object:
 
  class Signaler extends Thread{
      Waiter w;
      public Signaler(Waiter wtr){w = wtr;}
      public run(){
         wait_some_time
         signal();
      }
      void synchronized signal(){synchronized(w){w.notifyAll();}}
  }

Why is this necessary? Why isn't synchronizing signal() sufficient?

3) Is there a way to do broadcast notifys that will effect other
objects without passing object references around? In other words, if I
have n=unknown Waiter objects waiting can I notify them all with from
one generic Signaler?

Thanks,

-m


Michael Slominski      | The Lord's Prayer is 66 words, the Gettysburg 
Slominski Consulting   | Address is 286 words, there are 1,322 words in the 
408.738.1446           | Declaration of Independence, but government
michael@earthlight.com | regulations on the sale of cabbage total 26,911 words.
http://earthlight.com  |                                 -unknown
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post