Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IllegalMonitorStateException: current thread not owner

13 views
Skip to first unread message

Riaz Uddin Ahmed

unread,
Aug 10, 2003, 9:08:24 AM8/10/03
to
I've given the source code and the corresponding error generated by
JVM. Idea behind the program is there will be few Threads running
which will check a global data area whether has any data or not. If no
data is there, one of the Threads will put data and will notify all.
Otherwise the Thread will wait. Except these Thread, there will be
another thread which will check has data in global area or not. The
Thread will wait if has no data otherwise it will do something. I
tried my best to solve this situation. Please check my code & error
report and let me know

Thanks in advance.....

<<Code>>
public class Sender extends Thread
{
static String data = "";
static int machineId;

public static void main(String[] args)
{
SenderChild sc1 = new SenderChild(1);
Thread s1 = new Thread(sc1);
s1.start();
Sender sender = new Sender();
sender.start();
}
public void run()
{
while(true)
{
if( Sender.hasData() )
{
Sender.putData("");
notifyAll();
}
else
{
try
{wait(); }
catch(InterruptedException ie){}
}
}
}
static synchronized boolean hasData()
{
if(data == null || data.length() == 0)
return false;
return true;
}
static synchronized void putData(String data)
{
Sender.data = data;
}
static synchronized void machineNo(int id)
{
Sender.machineId = id;
}
}
class SenderChild implements Runnable
{
int i;
int index = 0;
public SenderChild(int id)
{
i = id;
}
public void run()
{
while(true)
{
System.out.println(""+i);
if(Sender.hasData())
{
try{wait();}
catch(InterruptedException ie)
{
}
}
else
{
Sender.putData("From machine no:- "+i+" for "+index+" times");
Sender.machineNo(i);
notifyAll();
}}}}


<<Error report>>
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at SenderChild.run(Sender.java:101)
at java.lang.Thread.run(Thread.java:536)
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at Sender.run(Sender.java:35)

William Brogden

unread,
Aug 10, 2003, 11:27:49 AM8/10/03
to

"Riaz Uddin Ahmed" <ria...@hotmail.com> wrote in message
news:ff06a02f.03081...@posting.google.com...

> I've given the source code and the corresponding error generated by
> JVM. Idea behind the program is there will be few Threads running
> which will check a global data area whether has any data or not. If no
> data is there, one of the Threads will put data and will notify all.
> Otherwise the Thread will wait. Except these Thread, there will be
> another thread which will check has data in global area or not. The
> Thread will wait if has no data otherwise it will do something. I
> tried my best to solve this situation. Please check my code & error
> report and let me know

Look at the java.lang.Object documentation - find the notifyAll
method. Read why notifyAll may throw an IllegalMonitorStateException.

David Zimmerman

unread,
Aug 10, 2003, 9:55:44 AM8/10/03
to
You need to be synchronized on the object on which you're calling wait()
or notify().

You are not calling notify() on the same object on which you're calling
wait().

Adam Maass

unread,
Aug 10, 2003, 10:04:50 AM8/10/03
to

wait() and notify()/notifyAll() must operate on an object on which the
current thread already owns the monitor... IE, from within a synchronized
block.

Let's see if I can cobble something up that matches your specification:

import java.util.*;

class Data
{
public static final Data INSTANCE = new Data();

private List elements = new LinkedList();

public synchronized boolean hasData()
{
return !elements.isEmpty();
}

public synchronized void putData(String s)
{
elements.add(s);
}

public synchronized String getData()
{
String s = (String)elements.get(0);
elements.remove(s);

return s;
}
}

class Sender implements Runnable
{
public void run()
{
int count = 0;

while(true)
{
synchronized(Data.INSTANCE)
{
while(Data.INSTANCE.hasData())
{
try
{
Data.INSTANCE.wait();
}
catch(InterruptedException e)
{
}
}

Data.INSTANCE.putData("String " + count);
Data.INSTANCE.notifyAll();
}

count++;
}
}
}

class Receiver implements Runnable
{
public void run()
{
while(true)
{
String s = null;

synchronized(Data.INSTANCE)
{
while(!Data.INSTANCE.hasData())
{
try
{
Data.INSTANCE.wait();
}
catch(InterruptedException e)
{
}
}

s = Data.INSTANCE.getData();
Data.INSTANCE.notifyAll();
}

System.out.println(s);
}
}
}


public class TestHarness
{


public static void main(String[] args)
{

new Thread(new Receiver()).start();
new Thread(new Sender()).start();
}
}


Roedy Green

unread,
Aug 14, 2003, 1:15:17 AM8/14/03
to
On 10 Aug 2003 06:08:24 -0700, ria...@hotmail.com (Riaz Uddin Ahmed)
wrote or quoted :

>notifyAll();

you can't do a notifyAll without having a lock on the object first.
See http://mindprod.com/jgloss/thread.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

0 new messages