File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes illegal monitor state Exception[soon as possible....] Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "illegal monitor state Exception[soon as possible....]" Watch "illegal monitor state Exception[soon as possible....]" New topic
Author

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

netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
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

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
 
IntelliJ Java IDE
 
subject: illegal monitor state Exception[soon as possible....]
 
Threads others viewed
Synchronization - static and non-static methods.
How to make a main thread wait until all other threads die out?
Trying to walk the (dir) tree.
Question on Thread
doubt in thread run's method
IntelliJ Java IDE