How to pause Thread execution

前端 未结 5 1228
耶瑟儿~
耶瑟儿~ 2021-01-03 09:09

How to pause execution of some Thread. I have Thread t and I have two buttons, PAUSE and CONTINUE. On pause I need to pause thread execution and on continue to thread start

5条回答
  •  感情败类
    2021-01-03 09:41

    You can try this:

    private boolean isPaused = false;
    
    
    public synchronized void pause(){
        isPaused = true;
    }
    
    public synchronized void play(){
       isPaused = false;
       notyfyAll();
    }
    
    public synchronized void look(){
       while(isPaused)
          wait();
    }
    
     public void run(){
         while(true){
            look();
            //your code
     }
    

提交回复
热议问题