Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

illegal monitor state Exception[soon as possible....]  RSS feed

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code snippet:
-------------
.....
...
int th=1;
thgp=new ThreadGroup("File_hunter");
if(flag==0)
{
if(v_path.length>0)
for(int i=0;i<v_path.length;i++)
{
if(v_path[i]!=null)
{
first=new Thread(thgp,this,("Thread"+th));
th++;
System.out.println("v_path: "+v_path[i]);
first.start();
}
}
if(v_ind!=1)
{
first=new Thread(thgp,this,("Master"+th));
th++;
first.start();
}
Thread tot=new Thread(thgp,this,"TotDel");
tot.start();
....................
...............
public void run()
{
String thname=Thread.currentThread().getName();
System.out.println("thread: "+thname);
if(thname.startsWith("Master"))
{
if(v_ind==1)
Dirlisting(v_path[i],chopt,0);
else
{
Dirlisting(arg1,chopt,0);
v_ind=1;
}
}//end of if.
else
if(thname.startsWith("To"))
{
try
{
wait();
}catch(Exception e){System.out.println(e);}
System.out.println("Deleting");
totdeleted();
}
}
......................
This is a piece of code,where I get an IllegalMonitorStateException.This is to display the files present in the given path including the subdirectories.Please help me to correct this as soon as possible.
Happy middling with java.
Netharam
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you call wait(), the current object's wait() method is invoked, ie, you are calling this.wait().
It looks like the current object doesn't have a monitor associated with it. You haven't obtained a lock on the current object previous to calling wait() which you must do. Since you've only provided snippets of code it's really impossible to suggest how to fix your problem correctly, but one thing you can do is add the synchronized keyword to your run() method. This will get rid of the exception you are getting but may introduce deadlock or starvation, since now only one thread at a time may execute the run() method of this Runnable object, and your design may or may not work like this.
[ March 07, 2002: Message edited by: Rob Ross ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!