When run exits the Thread instance will be garbage collected. You can
find out about this if you add a method like this:
protected void finalize() throws Throwable {
super.finalize();
System.out.println("finalized");
}
---
I think passing around the variable is bad but making a method which
sets the value of the variable to false seems good enough to me.
---
One thing that I find unusual about your code is that you start a
Thread for every hit of button 1 and if someone hits it more than once
then they will have the print out thing happening multiple times
simultaneously. Is that what you were looking to do? Hitting button 2
stops all the Threads started with button 1 hits.
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
> Hi all
>
[quoted text clipped - 20 lines]
> 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?
It goes into the terminated state.The Thread object can then be garbage
collected.
> 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
[quoted text clipped - 5 lines]
> going along the right lines? Any advice on this would be appreciated
> because I want to try and write **good** code.
You've got to keep it somewhere. You might want to start be
self-encapsulating it with get and set methods, and being explicit which
object you are calling those methods on.
> public class Main extends JFrame{
No need to extend JFrame.
> private boolean keepRunning = true;
You should at least make this variable volatile.
> private class oneAction extends AbstractAction implements Runnable {
Class names should have initial caps. It's a good idea for each class to
only do one thing, so use a separate class for the Runnable.
> public void actionPerformed(ActionEvent e) {
> new Thread(this).start();
> }
Depending upon what you want to do, you may find javax.swing.Timer
useful. In particular, you shouldn't modify the GUI off the Event
Dispatch Thread (EDT).
> Thread.sleep(100);
Sleeping means that the thread can't be told to exit (without an
interrupt, or worse). It's better to wait on a variable.
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
If you've been interrupted, that probably means you should stop what you
are doing. Don't just do the minimum to keep the compiler happy.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thanks for your help so far.
I did not really think about the one button being pressed multiple
times. It is only a test program for myself so it doesnt really matter.
I was thinking of using multiple classes and a variable to stop the
thread. Here is my design with a super class that controlls the
variable's value.
abstract wrapper class extends AbstractAction
| (with get/set methods for checkVar)
|
|------ StartAction (with ActionPerformed implemented
| and examines variable in super class to start)
|
|------ StopAction (with ActionPerformed implemented
and examines variable in super class to stop)
Is this clear?
Is this a good design?
I am just conscious about my design when it come to programming. I want
to get into good techniques now while Im still a relative beginner. If
anyone know of any good resources for design these would also be useful.
Thanks
Richard
Thanks for your input
Richard