[682] in java-interest

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

Monitor question ...

daemon@ATHENA.MIT.EDU (Jim Wright)
Thu Jul 13 06:35:48 1995

Date: Thu, 13 Jul 1995 10:22:35 +0100 (BST)
From: Jim Wright <jsw1003@hermes.cam.ac.uk>
To: java-interest@java.sun.com


Hi,

I have been experimenting with using threads in java, and have written a 
multi-threaded prime number generator to help my understanding.  But I 
seem to have hit a brick wall and am unsure whether it is me or the 
run-time system.  The source code I'm using is:

public class Example {
   public static void main(String args[]) {
      Pipe  output = new Pipe();
      int   n = 3;
      
      (new PrimeThread(output, 2)).start();
      while(n < 1000) {
	 output.push(n);
	 n += 2;
      }
   }
}

class PrimeThread extends Thread {
   Pipe  input, output = null;
   int   factor;

   public PrimeThread(Pipe in, int fac) {
      input = in;
      factor = fac;
   }

   public void run() {
      int  n;

      while(true) {
	 n = input.pop();
	 if (output == null) {
	    System.out.println(n);
	    output = new Pipe();
            (new PrimeThread(output, n)).start();
	 }
	 else
	    if (n % factor != 0) output.push(n);
      }
   }
}

class Pipe {
   int  buffer[] = new int[16], head = 0, tail = 0;

   public synchronized void push(int item) {
      while((head+1) % buffer.length == tail) wait();
      buffer[head] = item;
      head = (head+1) % buffer.length;
      notifyAll();
   }

   public synchronized int pop() {
      int  item;

      while(head == tail) wait();
      item = buffer[tail];
      tail = (tail+1) % buffer.length;
      notifyAll();

      return item;
   }
}

Now when I come to run this I get:

 .
 .
 .
271
277
281
java.lang.InternalException monitorNotifyAll(): current thread not owner
	at Pipe.push(Example.java:46)
	at PrimeThread.run(Example.java:34)
283
java.lang.InternalException monitorNotifyAll(): current thread not owner
	at Pipe.push(Example.java:46)
	at PrimeThread.run(Example.java:34)
java.lang.InternalException monitorWait(): current thread not owner
	at java.lang.Object.wait(Object.java)
	at Pipe.pop(Example.java:52)
	at PrimeThread.run(Example.java:27)
293
java.lang.InternalException monitorNotifyAll(): current thread not owner
	at Pipe.pop(Example.java:55)
	at PrimeThread.run(Example.java:27)
java.lang.InternalException monitorWait(): current thread not owner
	at java.lang.Object.wait(Object.java)
	at Pipe.pop(Example.java:52)
	at PrimeThread.run(Example.java:27)
java.lang.InternalException monitorExit(): current thread not owner
	at Pipe.pop(Example.java:57)
	at PrimeThread.run(Example.java:27)

The system hangs at this point.  I don't know if I haven't quite 
understood what synchronized methods are and how they interact with wait 
and notifyAll, or if the run-time is overloaded.  Any help is much 
appreciated.

Thanks

		Jim Wright
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

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