how to stop thread A from thread B using a button

Hi,

I have created a thread A which in turn creates a thread B.

Is it possible to press a button on thread B that stops execution of thread A (and consequently thread B, of course)?

More specifically, thread A is a client thread that waits for server data. If it takes too long I would like to give the opportunity to the user to stop waiting by pressing "cancel" button on thread B (which is actually a dialog box with a "please wait" message)

I can attach my code if requested.

[511 byte] By [xpantaa] at [2007-11-27 10:03:28]
# 1

What about interrupting it ?

http://java.sun.com/javase/6/docs/api/java/nio/channels/InterruptibleChannel.html

Something like:

private static final class B implements Runnable {

private final A a;

public B(A t) {

a = t;

}

public void run() {

int res = JOptionPane.showConfirmDialog(null, "Cancel waiting ?", "...", JOptionPane.CANCEL_OPTION);

if (res == JOptionPane.OK_OPTION)

a.interrupt();

}

}

ibanna at 2007-7-13 0:38:26 > top of Java-index,Core,Core APIs...
# 2

Thanks for your code. I am trying it right now...

The problem is that I actually want to interrupt B. Here is my code

class ThreadedAction extends AbstractAction implements Runnable{

public void actionPerformed(ActionEvent e){

obj = e.getSource();

if (worker != null)

return;

worker = new Thread(this);

worker.start();

}

public void run()

{

if (obj == SolveBtn)

{

try{

CylonBar bar = new CylonBar((JFrame)getRootPane().getParent());

bar.start(); // << bar is a threaded dialog box.

Result = nm.solveHEP(table); // <<-- nm is the object that communicates with the server waiting for a result. If it takes too long this thread should be stopped by the user

bar.setStopFlag(true);

}

catch(IOException ioe)

{}

}

worker = null;

}

}

and this is the code of the threaded dialog box

public class CylonBar extends Thread {

JProgressBar bar = new JProgressBar();

JFrame frame;

JLabel label = new JLabel("<html>

<b>Please Wait...</b></html>");

JButton CancelBtn = new JButton("Cancel");

volatile Boolean stopflag = false;

public CylonBar(JFrame parent)

{

frame = new JFrame();

frame.setSize(300, 200);

label.setForeground(Color.ORANGE);

frame.getContentPane().setBackground(Color.BLUE);

frame.getContentPane().setLayout(new BorderLayout(2,2));

frame.getContentPane().add(bar, BorderLayout.CENTER);

frame.setLocation(parent.getLocationOnScreen().x + parent.getWidth()/2 - frame.getSize().width/4, parent.getLocationOnScreen().y + parent.getHeight()/2 - frame.getSize().height/4);

frame.add(bar, BorderLayout.NORTH);

frame.add(label, BorderLayout.CENTER);

frame.add(CancelBtn, BorderLayout.SOUTH);

bar.setIndeterminate(true);

bar.setForeground(Color.GREEN);

frame.pack();

frame.setVisible(true);

CancelBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

Thread.currentThread().interrupt(); // <<-- it doesn't work!!!

}

});

}

public void run()

{

while (true)

{

try {

sleep(1000);

if(stopflag){

//Thread.currentThread().interrupt();

frame.setVisible(false);

}

} catch (InterruptedException e){}

}

}

public void setStopFlag(boolean t)

{

this.stopflag = t;

}

}

I would like to cancel the operation ThreadedAction is doing using a button on the threaded dialog box "CylonBar"

xpantaa at 2007-7-13 0:38:26 > top of Java-index,Core,Core APIs...
# 3

CancelBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

Thread.currentThread().interrupt(); // <<-- it doesn't work!!!

}

});

This will be called by the AWTThread i guess.

Got it ?

ibanna at 2007-7-13 0:38:26 > top of Java-index,Core,Core APIs...