views:

32

answers:

2

I am trying to figure out how to start and stop a serial interface.

class SerialInterface implements Runnable{




 // Stream from file
@Override
public void run(){
    try {
            startInterface();
        } catch (IOException ex) {
            Logger.getLogger(SerialInterface.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main( String StartStop ){
        SerialInterface SerialThread = null;

        if ( StartStop.equals("start")){
        SerialThread = new SerialInterface();
        Thread thread1 = new Thread (SerialThread);
        thread1.start();
        } else {
            SerialThread = null;
        }
    }

 private void startInterface() throws IOException {  //START DOING STUFF TO SERIAL PORT }

This does not work. How do I stop a thread which was started?

+1  A: 

thread1.isAlive() will return a boolean telling you whether it is alive or not.

Mike
A: 

Ok, I think i got this figured out... This main("start") creates a new instance of the thread, then when the main("stop") is called, it creates a new instance of the thread and nulls it out. So I need to bring the actual variable creation out of the thread.

Adam Outler