How to properly stop the Thread in Java?

后端 未结 9 1157
执念已碎
执念已碎 2020-11-21 16:19

I need a solution to properly stop the thread in Java.

I have IndexProcessorclass which implements the Runnable interface:

public class          


        
9条回答
  •  一整个雨季
    2020-11-21 16:22

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

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

提交回复
热议问题