Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > IllegalMonitorStateException: current thread not owner

Reply
Thread Tools

IllegalMonitorStateException: current thread not owner

 
 
Riaz Uddin Ahmed
Guest
Posts: n/a
 
      08-10-2003
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)
 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      08-10-2003
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().

 
Reply With Quote
 
 
 
 
William Brogden
Guest
Posts: n/a
 
      08-10-2003

"Riaz Uddin Ahmed" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.



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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-14-2003
On 10 Aug 2003 06:08:24 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ThreadError: not owner Thomas B. Ruby 9 09-22-2008 10:58 AM
IllegalMonitorStateException: how to make thread owner Madhur Ahuja Java 4 08-11-2007 11:42 AM
Thread.current.parent and Thread.current.ancestors ara.t.howard@noaa.gov Ruby 3 02-16-2007 03:34 PM
how to register comp licence to ME not old owner fredcromer Computer Information 2 04-11-2004 01:11 PM
How to change the current thread current culture at run time. Manu ASP .Net 1 07-20-2003 03:27 PM



Advertisments