IllegalMonitorStateException: how to make thread owner

M

Madhur Ahuja

Hello
I am making a simple multithreaded sorting program.
The main program instantiates a new thread which carries out sorting and
prints out the result. This requires the use of wait function as shown
below.

However the IllegalMonitorStateException occurs stating that current thread
is not
the owner of the object. I have tried all methods specified in the
documentation of
the *notify* function, but no avail.

What change should I make below in order to correct the error.

class qapp
{
public static void main(String args[]) throws InterruptedException
{
Thread t=Thread.currentThread();
qsort qr=new qsort(t);

System.out.println(t.toString());
qr.input();
qr.start();
t.wait(); // <-------------Exeception Line
System.out.println("\nThe sorted numbers are:");
qr.display();

}
}


Exception in thread "main" java.lang.IllegalMonitorStateException: current
threa
d not owner

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
C

Carl Howells

Madhur said:
What change should I make below in order to correct the error.

Call to wait() and notify() need to be inside of blocks synchronized on
the object being waited on or notified. So change:

t.wait()

to

synchronized (t)
{
t.wait()
}

Of course, without seeing the other logic in your program, I have no
clue whether this will make it work correctly or not. But, it will get
rid of the exception you're experiencing.
 
D

David Hilsee

Madhur Ahuja said:
Hello
I am making a simple multithreaded sorting program.
The main program instantiates a new thread which carries out sorting and
prints out the result. This requires the use of wait function as shown
below.

However the IllegalMonitorStateException occurs stating that current thread
is not
the owner of the object. I have tried all methods specified in the
documentation of
the *notify* function, but no avail.

What change should I make below in order to correct the error.

class qapp
{
public static void main(String args[]) throws InterruptedException
{
Thread t=Thread.currentThread();
qsort qr=new qsort(t);

System.out.println(t.toString());
qr.input();
qr.start();
t.wait(); // <-------------Exeception Line
System.out.println("\nThe sorted numbers are:");
qr.display();

}
}

From the code, it looks like you really want to call join(), not wait().
Object.wait() is used in combination with Object.notify() and
Object.notifyAll() to allow threads to pause until they are notified by
another thread. Thread.join() pauses the current thread until the other
thread has terminated. If that is the case, then the above code probably
doesn't need that background thread because the thread that is executing the
main method isn't doing any work while the background thread is running.
 
M

Madhur Ahuja

You are right, I must use join method rather that wait(). Thanks

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com



David Hilsee said:
Madhur Ahuja said:
Hello
I am making a simple multithreaded sorting program.
The main program instantiates a new thread which carries out sorting and
prints out the result. This requires the use of wait function as shown
below.

However the IllegalMonitorStateException occurs stating that current thread
is not
the owner of the object. I have tried all methods specified in the
documentation of
the *notify* function, but no avail.

What change should I make below in order to correct the error.

class qapp
{
public static void main(String args[]) throws InterruptedException
{
Thread t=Thread.currentThread();
qsort qr=new qsort(t);

System.out.println(t.toString());
qr.input();
qr.start();
t.wait(); // <-------------Exeception Line
System.out.println("\nThe sorted numbers are:");
qr.display();

}
}

From the code, it looks like you really want to call join(), not wait().
Object.wait() is used in combination with Object.notify() and
Object.notifyAll() to allow threads to pause until they are notified by
another thread. Thread.join() pauses the current thread until the other
thread has terminated. If that is the case, then the above code probably
doesn't need that background thread because the thread that is executing the
main method isn't doing any work while the background thread is running.
 
Joined
Aug 11, 2007
Messages
2
Reaction score
0
Can someone please tell me what is wrong in this java code?

/* The program is intended to start animating text at the click of a button, pause it at another click and resume at the next click. It should continue like this */

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class TextAnime implements Runnable
{
JFrame frame;
boolean flag;
Thread animeThread;
JLabel label;
String[] textArray;

public TextAnime()
{
flag = false;
animeThread = new Thread(this);

frame = new JFrame("Animate Text");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
frame.setLayout(gbl);

JButton button = new JButton("Start");
label = new JLabel("Stopped");

textArray = new String[5];
String textArray1[] = {"Programmer", "SportsMan", "Genius", "Friend", "Knowledgable"};
for(int ctr = 0; ctr<5 ; ctr++)
{
textArray[ctr] = textArray1[ctr];
}

gbc.weightx = 1;
gbc.gridx = 0;
gbl.setConstraints(button,gbc);
frame.getContentPane().add(button);

ButList bl = new ButList();
button.addActionListener(bl);

gbc.gridx = 1;
gbl.setConstraints(label,gbc);
frame.getContentPane().add(label);

frame.setSize(200,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public class ButList implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{

if(flag == false)
{
flag = true;

animeStart();

}

else
{
flag = false;
//animeStop();
}

}
}

public synchronized void animeStart()
{
animeThread.start();
Thread newThread;
newThread = Thread.currentThread();
newThread.notify();
}

public void animeStop()
{
animeThread.interrupt();
}

public void run()
{
int i = 0;
try
{
while(i == i)
{
if(i==5)
{
i=0;
}

label.setText(textArray);
animeThread.sleep(1000);
i++;

if (flag == false)
{

animeThread.wait();
}

}
}
catch(InterruptedException ie)
{
label.setText("Stopped");
}
}

public static void main(String args[])
{
TextAnime ta = new TextAnime();

}
}


Please tell me if this can be written in a more simpler manner. Also please correct this code. I am tired after trying many times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top