java.lang.IllegalMonitorStateException: current thread not owner

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:

publicclass Ringenimplements Buffer

{

privatestaticfinalint BUFFER_SIZE = 10;

int in, out;// peker p?f鴕ste 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];

}

publicvoid 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;

publicclass Generateextends Thread

{

private Random genr =new Random();

private String[] liste =new String[10];

private Ringen buffer;

privateint 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";

}

publicvoid 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 :)

[5088 byte] By [kongsberga] at [2007-10-3 8:14:37]
# 1
also tried synchronized in fron of the two methods in Ringen. That just made the first one to use it stay in there forever...
kongsberga at 2007-7-15 3:19:32 > top of Java-index,Java Essentials,New To Java...
# 2
why is everyone ignoring me? heeeeelp! hehe ;-)
kongsberga at 2007-7-15 3:19:32 > top of Java-index,Java Essentials,New To Java...
# 3
You are calling wait() without getting relevant object monitor.
hiwaa at 2007-7-15 3:19:32 > top of Java-index,Java Essentials,New To Java...
# 4

thanx alot for the replay... thought no1 would answer... even wirh all that dukedollar;) hehe

yes, that what I found out to when i googlet it.. but didn't find any way to get he monitor... i thought first thread in the method would get it?

an using synchronized to on the method would be wrong wouldn't it?

kongsberga at 2007-7-15 3:19:32 > top of Java-index,Java Essentials,New To Java...
# 5
> using synchronized to on the methodThat should suffice for your ring buffer class.The wait() call should be complemented with notify() or notifyAll() calls on the same monitor otherwise your wait() blocks forever.
hiwaa at 2007-7-15 3:19:32 > top of Java-index,Java Essentials,New To Java...