Code Analysis of Java Thread State Transformation Process

Thread state

  • NEW: just created an unstarted thread
  • RUNNABLE: Executing state
  • BLOCKED: Thread in a blocked state
  • WAITING: A thread that is waiting for another thread to perform a specific action
  • TIMED_WAITING: Waiting for the execution time of another thread to reach the specified time
  • TERMINATED: The thread exits execution
public class TestState {
    
    
  public static void main(String[] args) {
    
    
    Thread thread = new Thread(()->{
    
    
      for (int i = 0; i < 5; i++) {
    
    
        try {
    
    
          Thread.sleep(1000);
        } catch (InterruptedException e) {
    
    
          e.printStackTrace();
        }
      }
      System.out.println("/");
    });

    //观察线程状态
    Thread.State state = thread.getState();
    System.out.println(state); //New状态

    thread.start();
    state = thread.getState();
    System.out.println(state);//Run状态

    while (state!=Thread.State.TERMINATED){
    
    
      try {
    
    
        Thread.sleep(100);
      } catch (InterruptedException e) {
    
    
        e.printStackTrace();
      }
      state = thread.getState();//更新线程状态
      System.out.println(state);//输出状态
    }
  }
}

Thread Polite

The currently executing thread is suspended, but it will not be blocked. The
current thread loses its processor and is ready for programming.
Whether the comity is successful depends on the CPU. If the comity is successful, it will wait for the next scheduling.

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()+"线程停止执行");
  }
}

Results of the:

image

The thread is forced to the end

  • Use join() method
  • The thread using the join() method will be forced to execute until the end, and will not give up the processor
public class TestJoin implements Runnable{
    
    
  @Override
  public void run() {
    
    
    for (int i = 0; i < 1000; i++) {
    
    
      System.out.println("强制执行线程来了"+i);
    }
  }

  public static void main(String[] args) throws Exception{
    
    
    TestJoin testJoin = new TestJoin();
    Thread thread = new Thread(testJoin);
    thread.start();

    for (int i = 0; i < 500; i++) {
    
    
      if(i==200){
    
    
        thread.join();
      }
      System.out.println("主线程"+i);
    }
  }
}

Life is more than perseverance and hard work. Dreams are meaningful pursuits.
Finally, I wish you all early success in your studies, get a satisfactory offer, get a quick promotion and raise your salary, and reach the pinnacle of life.
If you need courseware source code software and other materials to add a small assistant vx: xcw18874131605 (note: CSDN)

Guess you like

Origin blog.csdn.net/p1830095583/article/details/115255387