Greetings,
I am trying to do some work with threads in Swing. I have simplified my problem in the following example. What I am trying to do is simply suspend and resume a thread’s execution by pressing a series of buttons.
The frame contains three buttons. One starts the thread, the other causes the thread to pause using the wiat() method and the third using the notifyAll() to have the thread resume.
I have a class DoWork() that is implements the Runnable() interface that does the work.
What happens is I press the Start button and everything is fine. However, when I press the Wait button I get an java.lang.IllegalMonitorStateException: current thread not owner.
Does anybody know what is causing this?
Can anybody see anything wrong with this?
Thank you in advance
Jeff Sulman
Here is the code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.swing.JFrame;
public class Frame extends JFrame {
Thread t = null;
DoWork doWork = new DoWork();
JButton buttonStart= new JButton("Start");
JButton buttonWait = new JButton("Wait");
JButton buttonResume = new JButton("Resume");
public Frame(String str){
super(str);
setSize(400, 150);
Container content = getContentPane();
content.setLayout(new FlowLayout());
// Start Button
content.add(buttonStart);
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
startOperation();
}
});
// Wait Button
content.add(buttonWait);
buttonWait.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
waitOperation();
}
});
// Resume Button
content.add(buttonResume);
buttonResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
resumeOperation();
}
});
setVisible(true);
}
// Start button pressed
public synchronized void startOperation(){
t = new Thread(doWork);
System.out.println("Starting Thread");
t.start();
System.out.println("Started Thread");
}
// Wait button pressed
public synchronized void waitOperation(){
try{
System.out.println("Thread waiting");
t.wait();
} catch (InterruptedException e){
}
}
// Resume button pressed
public synchronized void resumeOperation(){
t.notifyAll();
System.out.println("Thread waiting");
}
public static void main(String[] args) {
Frame f = new Frame("This is a test");
}
public class DoWork implements Runnable{
public void run(){
for(int i = 0; i < 10000000; i++){
for(int j = 0; j < 10000000; j++){
for(int k = 0; k < 10000000; k++){
}
}
}
}
}
}
I am trying to do some work with threads in Swing. I have simplified my problem in the following example. What I am trying to do is simply suspend and resume a thread’s execution by pressing a series of buttons.
The frame contains three buttons. One starts the thread, the other causes the thread to pause using the wiat() method and the third using the notifyAll() to have the thread resume.
I have a class DoWork() that is implements the Runnable() interface that does the work.
What happens is I press the Start button and everything is fine. However, when I press the Wait button I get an java.lang.IllegalMonitorStateException: current thread not owner.
Does anybody know what is causing this?
Can anybody see anything wrong with this?
Thank you in advance
Jeff Sulman
Here is the code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.swing.JFrame;
public class Frame extends JFrame {
Thread t = null;
DoWork doWork = new DoWork();
JButton buttonStart= new JButton("Start");
JButton buttonWait = new JButton("Wait");
JButton buttonResume = new JButton("Resume");
public Frame(String str){
super(str);
setSize(400, 150);
Container content = getContentPane();
content.setLayout(new FlowLayout());
// Start Button
content.add(buttonStart);
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
startOperation();
}
});
// Wait Button
content.add(buttonWait);
buttonWait.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
waitOperation();
}
});
// Resume Button
content.add(buttonResume);
buttonResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
resumeOperation();
}
});
setVisible(true);
}
// Start button pressed
public synchronized void startOperation(){
t = new Thread(doWork);
System.out.println("Starting Thread");
t.start();
System.out.println("Started Thread");
}
// Wait button pressed
public synchronized void waitOperation(){
try{
System.out.println("Thread waiting");
t.wait();
} catch (InterruptedException e){
}
}
// Resume button pressed
public synchronized void resumeOperation(){
t.notifyAll();
System.out.println("Thread waiting");
}
public static void main(String[] args) {
Frame f = new Frame("This is a test");
}
public class DoWork implements Runnable{
public void run(){
for(int i = 0; i < 10000000; i++){
for(int j = 0; j < 10000000; j++){
for(int k = 0; k < 10000000; k++){
}
}
}
}
}
}