簡體   English   中英

如何啟動/停止和監視Java線程?

[英]How to start/stop and monitor a java thread?

我試圖弄清楚如何啟動和停止串行接口。

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()將返回一個布爾值,告訴您它是否還存在。

好的,我想我已經弄清楚了...這個main(“ start”)創建線程的新實例,然后當main(“ stop”)被調用時,它創建線程的新實例並將其為空出來。 因此,我需要將實際的變量創建帶出線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM