How do you kill a Thread in Java?

前端 未结 16 2035
天命终不由人
天命终不由人 2020-11-21 05:54

How do you kill a java.lang.Thread in Java?

16条回答
  •  梦如初夏
    2020-11-21 06:50

    I didn't get the interrupt to work in Android, so I used this method, works perfectly:

    boolean shouldCheckUpdates = true;
    
    private void startupCheckForUpdatesEveryFewSeconds() {
        Thread t = new Thread(new CheckUpdates());
        t.start();
    }
    
    private class CheckUpdates implements Runnable{
        public void run() {
            while (shouldCheckUpdates){
                //Thread sleep 3 seconds
                System.out.println("Do your thing here");
            }
        }
    }
    
     public void stop(){
            shouldCheckUpdates = false;
     }
    

提交回复
热议问题