麦麦脆汁鸡

导航

线程状态

线程状态

创建状态、就绪状态、运行状态、阻塞状态、死亡状态

image-20220318154143888

image-20220318154530291

 

线程停止

package com.thread.state;

//测试stop
//1.建议线程正常停止——>利用次数,不建议死循环
//2.建议使用标志位——>设置一个标志位

public class TestStop implements Runnable{

   //1.设置一个标志位
   private boolean flag = true;

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

   //2.设置一个公开的方法停止线程,转换标志位
   public void stop(){
       this.flag = false;
  }

   public static void main(String[] args) {

       TestStop testStop = new TestStop();

       new Thread(testStop).start();

       for (int j = 0; j < 1000; j++) {
           System.out.println("main"+j);
           if(j==90){
               testStop.stop();//调用stop方法,切换标志位,让线程停止
               System.out.println("线程停止");
          }
      }

  }


}

 

线程休眠_sleep

  • sleep (时间)指定当前线程阻塞的毫秒数,1000毫秒=1秒;

  • sleep存在异常InterruptedException;

  • sleep时间达到后线程进入就绪状态;

  • sleep可以模拟网络延时,倒计时等;

  • 每一个对象都有一个锁, sleep不会释放锁。

package com.thread.state;

//模拟倒计时
public class TestSleep2 {

   public static void main(String[] args) {
       try {
           tenDown();
      } catch (InterruptedException e) {
           e.printStackTrace();
      }
  }

   //模拟倒计时
   public static void tenDown() throws InterruptedException {
       int num = 10;

       while(true){
           Thread.sleep(1000);
           System.out.println(num--);
           if (num<=0){
               break;
          }

      }
  }

}

 

线程礼让_yield

  • 礼让线程:当前正在执行的线程暂停(A),但不阻塞

  • 线程(A)从运行状态转为就绪状态

  • 让CPU重新调度(A和B),但再次调度还是看CPU心情让谁先进,所以礼让不一定成功!!

package com.thread.state;

//礼让线程
//礼让不一定成功,看CPU心情
public class TestYield {

   public static void main(String[] args) {
       MyYield myYield = new MyYield();

       new Thread(myYield,"a").start();
       new Thread(myYield,"b").start();
  }

}

class MyYield implements Runnable{
   @Override
   public void run() {
       System.out.println(Thread.currentThread().getName()+"线程开始执行");
       Thread.yield();//礼让
       System.out.println(Thread.currentThread().getName()+"线程停止执行");
  }
}

 

线程强制执行_join

  • 可以想象成插队

  • 待此线程执行完后,再执行其他线程

package com.thread.state;

import com.exception.demo01.Test;

//测试join方法
public class TestJoin implements Runnable{
   @Override
   public void run() {
       for (int i = 0; i < 1000; i++) {
           System.out.println("线程VIP来了"+i);
      }
  }


   public static void main(String[] args) throws InterruptedException {

       //启动我们的线程
       TestJoin testJoin = new TestJoin();
       Thread thread = new Thread(testJoin);
       
       //主线程
       for (int j = 0; j < 500; j++) {
           if(j==200){
               thread.start();
               thread.join();//插队
          }
           System.out.println("main"+j);
      }

  }

}

 

观测线程状态

package com.thread.state;

//观察测试线程的状态
public class TestState {

   //线程体
   public static void main(String[] args) throws InterruptedException {
       Thread thread = new Thread(()->{
           for (int i = 0; i < 5; i++) {
               try {
                   Thread.sleep(1000);
              } catch (InterruptedException e) {
                   e.printStackTrace();
              }
          }
           System.out.println("/////////");
      });


       //观察状态(目前应该是new)
       Thread.State state = thread.getState();
       System.out.println(state);//new


       //观察启动后(应该是run)
       thread.start();//启动线程
       state = thread.getState();
       System.out.println(state);//run


       //只要线程不终止,就一直输出状态
       while(state!=Thread.State.TERMINATED){
           Thread.sleep(100);//每0.1秒更新一次状态

           state = thread.getState();//更新线程状态
           System.out.println(state);//输出状态
      }
       
       
  }

}

 

posted on 2022-03-20 16:26  麦麦脆汁鸡  阅读(145)  评论(0编辑  收藏  举报