线程的暂停

线程的五大状态分别有,创建状态,就绪状态,运行状态,阻塞状态,和死亡状态

image-20210904214152591

image-20210904214237603

package BufferedTest;

public class TestStop1 implements Runnable{

  private boolean flag = true;
  @Override
  public void run() {
      int i = 0;
      while (flag){
          System.out.println("Run.....Thread"+i++);
      }

  }
  public void stop(){
      this.flag = false;
  }

  public static void main(String[] args) {

      TestStop1 s1 = new TestStop1();

      new Thread(s1).start();

      for (int i = 0; i < 1000; i++) {

          System.out.println("main"+i);
          if(i == 900){
              s1.stop();
              System.out.println("线程该停止了");
          }
      }

  }
}
posted @ 2021-09-04 21:43  πππ·  阅读(57)  评论(0)    收藏  举报