我试图弄清楚如何启动和停止串行接口。

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 }


这是行不通的。如何停止已启动的线程?

最佳答案

thread1.isAlive()将返回一个布尔值,告诉您它是否还存在。

09-30 13:28