Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread is not stopped, even after it is interrupted.  RSS feed

 
Vijay Venkat
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got the following problem in implementing the "Abort window" for a big process. Here i have simulated that. The problem follows:
I have created a window and there is a button in that window. When I clicked that button, a process should run in the background and the abort window should come. If I "Abort" that abort window by clicking the 'abort' button or 'Esc' button, the thread Should be stopped and the abort window should disappear and I should get my first window(which is having the button). Here I have used the function "interrupt()" for interrupting the thread. The thread is not being stopped, when I abort the process by click 'Abort' button or 'Esc' button, but the 'Abort Window' disappeared, and after the Thread ends up, it trys for disposing the abort Dialog and gives exception as I have given.
Please try this code and send me your great suggestions.
The CODE is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class ThreadDialog extends JFrame implements ActionListener{
private Thread m_clickThread;
private boolean m_isAborted = false;
private AbortBtnDialog m_abortDialog = null;
public ThreadDialog() {
createWindow();
}
private void createWindow() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
button.addActionListener(this);
this.getContentPane().add(button);
pack();
show();
}
private void invokeFunction() throws InterruptedException {
outerFuction(); //Calls a very big External Operation
if (m_abortDialog != null) {
m_abortDialog.dispose();
}
}
private void outerFuction() throws InterruptedException {
//For testcode, it has been written inside the class itself
//This simulates a big process (will continue for 1 to 10 sec. depends on the processor)
for (int i = 0; i < 10000; i++) {
try {
RandomAccessFile raf = new RandomAccessFile("ThreadDialog.java","r");
raf.close();
} catch (Exception theException) {
theException.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent theActionEvent) {
m_clickThread = new Thread() {
public void run() {
try {
invokeFunction();
} catch (InterruptedException theInterruptedException) {
theInterruptedException.printStackTrace();
}
}
};
m_clickThread.start();
m_abortDialog = new AbortBtnDialog(JOptionPane.getFrameForComponent(this));
m_abortDialog.show();
}
public static void main(String s[]) {
new ThreadDialog();
}

private class AbortBtnDialog extends JDialog {
public AbortBtnDialog(Frame theParent) {
super(theParent, true);
this.setTitle("Abort Window");
JPanel abortPanel = new JPanel(new GridBagLayout());
JLabel infoLbl = new JLabel("Fetch in Progress");
JButton abortButton = new JButton("Abort");
abortPanel.add(infoLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(12, 12, 11, 5),
0, 0));
abortPanel.add(abortButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(12, 0, 11, 11),
0, 0));
this.getContentPane().add(abortPanel);
this.pack();
abortButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent theActionEvent) {
executeAbortAction();
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent theWindowEvent) {
executeAbortAction();
}
});
this.getRootPane().setDefaultButton(abortButton);
abortPanel.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent theActionEvent) {
m_isAborted = true;
m_clickThread.interrupt();
AbortBtnDialog.this.dispose();
}
},
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
}
private void executeAbortAction() {
m_isAborted = true;
m_clickThread.interrupt();
AbortBtnDialog.this.dispose();
}
}
}
-----
CODE ends.
I got the following exception:

Disposal was interrupted:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:420)
at java.awt.EventQueue.invokeAndWait(EventQueue.java:527)
at java.awt.Window.dispose(Window.java:481)
at java.awt.Dialog.disposeImpl(Dialog.java:432)
at java.awt.Dialog.dispose(Dialog.java:428)
at ThreadDialog.invokeFunction(ThreadDialog.java:31)
at ThreadDialog.access$000(ThreadDialog.java:7)
at ThreadDialog$1.run(ThreadDialog.java:52)
Thank you.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
simply calling a thread to be interrupted is not enough. There are two ways to stop a thread, the first is the warst, you could call Thread.stop().
This function is deprecated, as I know and it doesn't work under some OS.
The second is that you create for your Thread a boolean member, like bStopThread, which is false all the time.
If you press your button to stop the thread, you set this member to true. And last you ask in your run method, where your for or while is situated, if bStopThread is true. If it is, then you have to finish with a break or something else.
Hope that's clear
Rene
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!