1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

java.lang.IllegalMonitorStateException: current thread not owner

Discussion in 'Software Development' started by gdi1942, Jun 28, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. gdi1942

    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)
     
  2. Chicon

    Chicon

    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.
     
  3. gdi1942

    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.
     
  4. Chicon

    Chicon

    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.
     
  5. gdi1942

    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.
     
  6. Chicon

    Chicon

    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.
     
  7. gdi1942

    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
     
  8. Sponsor

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 733,556 other people just like you!

Loading...
Similar Threads - java lang IllegalMonitorStateException
  1. Joe Hudson
    Replies:
    4
    Views:
    893
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/589709

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice