# java.lang.IllegalMonitorStateException: current thread not owner



## gdi1942

I've had this before and I don't remember the concept behind fixing it. I want my method to wait until execution on another thread stops, the one started just before it.



Code:


	public void addFiles(File[] theFiles)
	{	
		GUI thisGUI = this;
		FileParserThread thread = new FileParserThread(theFiles,thisGUI);
		thread.start();
		try {
			wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		refresh();
	}

Console outputs:



Code:


Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread not owner
	at java.lang.Object.notify(Native Method)
	at GUI$FileParserThread.run(GUI.java:1166)


----------



## Chicon

Hi gdi1942,

The exception message doesn't seem to be directly related to the piece of coding you posted.
It has been produced by a *notify* in the *run* method of your *FileParserThread* class. The *run* method has been itself invoked by the JVM when the *thread.start();* statement was being executed.

To summarize, your thread is not the owner of the object's monitor on which it's waiting.

On this page, in the detailed explanations of the *notify* method, you may find a way to solve the problem.


----------



## gdi1942

Alright i worked around that one, but i'm getting it again



Code:


			for(int i=0 ; i!=files.length ; i++)
			{
				do
				{
				theGui.getUserEntry().setVisible(true);
				theGui.getTxtRename().setText(((File)files[i]).getName());
				theGui.getTxtFilename().setText(((File)files[i]).getName());
				try {
					theGui.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				s = theGui.getTxtRename().getText();
				}
				while ((s== null) || (s.equalsIgnoreCase("")) || (!(s.substring(s.length()-4,s.length()).equalsIgnoreCase(".mp3"))));
				File currentFile = (File) files[i];
				File newFile = new File(currentFile.getParent() + "\\" + s);
				currentFile.renameTo(newFile);
				System.out.println(newFile);

			}

This code is designed to show my user entry dialog box. Set the text inside to the filename that is to be renamed and allow for entry in the text box by waiting. However the wait is throwing that same error on wait() command.


----------



## Chicon

Hi gdi1942,

The wait() statement will always throw an exception when it is not enclosed in a synchronized method as 'synchronized' provides a monitor on the object shared by different threads.
Making a thread to wait for an undefine time implies that another thread will awake it by a notify.
Also, a Java application has always a main thread running. When it launches another thread, it is recommanded to 'join' the new thread as it is not always possible to determine if the new thread will end before the main thread.
There's a good article about Java multithreading on this page and followings with examples.


----------



## gdi1942

the method block is syncronized. And yes an action listenener in the prompt will call the notifiy.


----------



## Chicon

I would like to know the coding of the *actionPerformed* method of the listener.

BTW, personally, when I have to build a multi-thread application, I use a separate class implemented with all the synchronized methods and I use the singleton facilities for the first instantiation of the class to make sure the different threads are sharing the same object.


----------



## gdi1942

I haven't coded it yet. i was hoping it would just pause and show me the window. But i get this


----------

