R
Richard
Hi all
I was looking at some code about threads and am a bit stuck. Any advice
will be appreciated. I have two questions.
First the background....
I have two buttons on a form called one and two. When you click button
one it outputs the numbers 0 to 99 every tenth of a second. THis is on
a separate thread. I have a second button which is used to cancel the
buttonOne action. I have done this with a variable check as recommended
by Sun in the Thread class file under the stop method
"Many uses of <code>stop</code> should be replaced by code that simply
modifies some variable to indicate that the target thread should stop
running. The target thread should check this variable regularly, and
return from its run method in an orderly fashion if the variable
indicates that it is to stop running."
And now the questions....
My first question is what happens to this thread when I return from the
run method? Am I correct in thinking that when the run method of a
thread is left it goes into the dead state and will just die?
My second question is about the checker boolean that Sun recommends
using to stop a thread. This works in my example because I am using two
inner classes so both the start and stop button actions can see the
'keepRunning' variable. Is there a pattern or an example of how I
could do this will three separate classes. I was thinking of passing
the variable around and checking it but this seemed to have quite a lot
of coupling. Is there a listener interface I could use for it? Am I
going along the right lines? Any advice on this would be appreciated
because I want to try and write **good** code.
Here is the code I have written to demostrate..
------------------------------------------------------------------
package test;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame{
private boolean keepRunning = true;
public Main() {
JPanel panel = new JPanel();
JButton one = new JButton();
one.setAction(new oneAction());
JButton two = new JButton();
two.setAction(new twoAction());
panel.add(one);
panel.add(two);
getContentPane().add(panel);
setSize(100, 100);
setLocation(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class oneAction extends AbstractAction implements Runnable {
public oneAction(){
super("one");
}
public void actionPerformed(ActionEvent e) {
new Thread(this).start();
}
public void run() {
for (int i = 0; i < 100; i++){
if (keepRunning){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(">>" + i);
} else {
return;
}
}
}
}
private class twoAction extends AbstractAction {
public twoAction(){
super("two");
}
public void actionPerformed(ActionEvent e) {
keepRunning = false;
System.out.println(">>" + "should stop button one action");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Main();
}});
}
}
---------------------------------------------------------------
Thanks for any help
Richard
--
I was looking at some code about threads and am a bit stuck. Any advice
will be appreciated. I have two questions.
First the background....
I have two buttons on a form called one and two. When you click button
one it outputs the numbers 0 to 99 every tenth of a second. THis is on
a separate thread. I have a second button which is used to cancel the
buttonOne action. I have done this with a variable check as recommended
by Sun in the Thread class file under the stop method
"Many uses of <code>stop</code> should be replaced by code that simply
modifies some variable to indicate that the target thread should stop
running. The target thread should check this variable regularly, and
return from its run method in an orderly fashion if the variable
indicates that it is to stop running."
And now the questions....
My first question is what happens to this thread when I return from the
run method? Am I correct in thinking that when the run method of a
thread is left it goes into the dead state and will just die?
My second question is about the checker boolean that Sun recommends
using to stop a thread. This works in my example because I am using two
inner classes so both the start and stop button actions can see the
'keepRunning' variable. Is there a pattern or an example of how I
could do this will three separate classes. I was thinking of passing
the variable around and checking it but this seemed to have quite a lot
of coupling. Is there a listener interface I could use for it? Am I
going along the right lines? Any advice on this would be appreciated
because I want to try and write **good** code.
Here is the code I have written to demostrate..
------------------------------------------------------------------
package test;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame{
private boolean keepRunning = true;
public Main() {
JPanel panel = new JPanel();
JButton one = new JButton();
one.setAction(new oneAction());
JButton two = new JButton();
two.setAction(new twoAction());
panel.add(one);
panel.add(two);
getContentPane().add(panel);
setSize(100, 100);
setLocation(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class oneAction extends AbstractAction implements Runnable {
public oneAction(){
super("one");
}
public void actionPerformed(ActionEvent e) {
new Thread(this).start();
}
public void run() {
for (int i = 0; i < 100; i++){
if (keepRunning){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(">>" + i);
} else {
return;
}
}
}
}
private class twoAction extends AbstractAction {
public twoAction(){
super("two");
}
public void actionPerformed(ActionEvent e) {
keepRunning = false;
System.out.println(">>" + "should stop button one action");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Main();
}});
}
}
---------------------------------------------------------------
Thanks for any help
Richard
--