java.lang.IllegalMonitorStateException: current thread not owner

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

gdi1942

Thread Starter
Joined
Nov 1, 2006
Messages
215
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)
 
Joined
Jul 29, 2004
Messages
6,650
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

Thread Starter
Joined
Nov 1, 2006
Messages
215
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.
 
Joined
Jul 29, 2004
Messages
6,650
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

Thread Starter
Joined
Nov 1, 2006
Messages
215
the method block is syncronized. And yes an action listenener in the prompt will call the notifiy.
 
Joined
Jul 29, 2004
Messages
6,650
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

Thread Starter
Joined
Nov 1, 2006
Messages
215
I haven't coded it yet. i was hoping it would just pause and show me the window. But i get this
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top