5 Replies Latest reply: Oct 26, 2006 5:52 AM by 800351 RSS

    java.lang.IllegalMonitorStateException: current thread not owner

    807598
      HI!
      N00b need help =)

      Its an producer consumer thread program that is surpouse to me synchronized.
      Here is what I thougt would be the way:
      public class Ringen implements Buffer
      {
           private static final int BUFFER_SIZE = 10;
           int in, out;  // peker p? f?rste ledige og siste okuperte.
           int count;    // Teller hvor mange objekter som er i bufferen.
           private Object[] buffer;
           
           public Ringen()
           {
                count = 0;
                in = 0;
                out = 0;
                
                buffer = new Object[BUFFER_SIZE];
           }
           
           public void insert(Object item)
           {
                while (count == BUFFER_SIZE){
                     try{
                          wait(); // If buffer is full put thread on waitinglist
                     }//end try
                     catch(InterruptedException e){
                          System.out.println("ERERERR"+e);
                     }
                }//end while 
                
                // Legger til et object i bufferen
                ++count;
                buffer[in] = item;
                in = (in + 1)% BUFFER_SIZE;
           }
           
           public Object remove()
           {
                Object item;
                
                while (count == 0){
                     try{
                          wait();
                     }//end try
                     catch(InterruptedException e){
                          System.out.println("ewwerwerwe"+e);
                     }
                }//end while
                
                --count;
                item = buffer[out];
                out = (out + 1) % BUFFER_SIZE;
                
                return item;
           }     
      }
      
      **********************************************************************************
      
      import java.util.Random;
      
      public class Generate extends Thread
      {
           private Random genr = new Random();
           private String[] liste = new String[10];
           private Ringen buffer;
           private int nr;
           
           
           public Generate(Ringen ring)
           {
                buffer = ring;
                
                liste[0] = "Iron Maiden";
                liste[1] = "Killers";
                liste[2] = "The Number Of The Beast";
                liste[3] = "Piece Of Mind";
                liste[4] = "Powerslave";
                liste[5] = "Life After Death";
                liste[6] = "Somewhere In Time";
                liste[7] = "Seventh Son Of The Seventh Son";
                liste[8] = "No Prayer For The Dying";
                liste[9] = "Fear Of The  Dark";
           }
           
           public void run()
           {
                while(true){
                          nr = genr.nextInt(10);
                          buffer.insert(liste[nr]);
                }//end while
           }
      
      }
      ]


      That is the buffer and one of the producers...
      When I run it this error message comes up:

      java.lang.IllegalMonitorStateException: current thread not owner.

      everytime eithera producer or consumer cals one of the two methods in Ringen I get one of those on the method call...

      So... please help :)