线程在其生命周期中会经历各个阶段。例如,一个线程诞生、启动、运行,然后消亡。下图展示了线程的完整生命周期。

Java 线程生命周期与示例

以下是生命周期的各个阶段

  • New - 新线程在新状态下开始其生命周期。它保持这种状态直到程序启动线程。它也被称为出生线程

  • Runnable - 新出生的线程启动后,该线程变得可运行。处于此状态的线程被认为正在执行其任务。

  • Running- 有时,线程在等待时会转换到等待状态另一个线程来执行任务。仅当另一个线程向等待线程发出继续执行的信号时,线程才会转换回可运行状态。

  • Waiting- 可运行线程可以进入指定时间间隔的定时等待状态。当该时间间隔到期或正在等待的事件发生时,处于此状态的线程将转换回可运行状态。

  • Dead - 可运行线程在完成其任务或以其他方式终止时进入终止状态。

示例 1

在此示例中,我们正在创建通过继承 Thread 类来创建两个线程。我们正在打印线程的每个状态。当线程对象被创建时,其状态为NEW;当调用start()方法时,状态为START;当run()方法被调用时,状态为RUNNING;当线程完成对 run() 方法的处理时,它会进入 DEAD 状态。

package com.yxjc123;
class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;
   ThreadDemo( String name) {
      threadName = name;
      System.out.println("Thread: " + threadName + ", " + "State: New");
   }
   public void run() {
      System.out.println("Thread: " + threadName + ", " + "State: Running");
      for(int i = 4; i > 0; i--) {
         System.out.println("Thread: " + threadName + ", " + i);
      }
      System.out.println("Thread: " + threadName + ", " + "State: Dead");
   }
   public void start () {
      System.out.println("Thread: " + threadName + ", " + "State: Start");
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
public class TestThread {
   public static void main(String args[]) {
      ThreadDemo thread1 = new ThreadDemo( "Thread-1");
      ThreadDemo thread2 = new ThreadDemo( "Thread-2");
      thread1.start();
      thread2.start();
   }   
} 

输出

Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, 3
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-2, State: Dead
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-1, 1
Thread: Thread-1, State: Dead 

示例 2

在这个示例中,我们'使用 sleep() 方法在处理中引入一些延迟并展示使用线程的并行处理的情况。我们通过继承 Thread 类来创建两个线程。我们正在打印线程的每个状态。当线程对象被创建时,其状态为NEW;当调用start()方法时,状态为START;当run()方法被调用时,状态为RUNNING;如果调用 sleep(),则线程进入 WAITING 状态;当线程完成对 run() 方法的处理时,它会进入 DEAD 状态。

package com.yxjc123;
class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;
   ThreadDemo( String name) {
      threadName = name;
      System.out.println("Thread: " + threadName + ", " + "State: New");
   }
   public void run() {
      System.out.println("Thread: " + threadName + ", " + "State: Running");
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            //让线程休眠一段时间。
            System.out.println("Thread: " + threadName + ", " + "State: Waiting");
            Thread.sleep(50);
         }
      } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
      }
      System.out.println("Thread: " + threadName + ", " + "State: Dead");
   }

   public void start () {
      System.out.println("Thread: " + threadName + ", " + "State: Start");
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
public class TestThread {
   public static void main(String args[]) {
      ThreadDemo thread1 = new ThreadDemo( "Thread-1");
      ThreadDemo thread2 = new ThreadDemo( "Thread-2");
      thread1.start();
      thread2.start();
   }   
} 

输出

Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, State: Waiting
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-2, State: Waiting
Thread: Thread-1, State: Waiting
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Waiting
Thread: Thread-2, 1
Thread: Thread-2, State: Waiting
Thread: Thread-1, 1
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Dead
Thread: Thread-1, State: Dead 

示例 3

在这个示例中,我们通过实现 Runnable 接口来创建两个线程。我们正在打印线程的每个状态。当线程对象被创建时,其状态为NEW;当调用start()方法时,状态为START;当run()方法被调用时,状态为RUNNING;如果调用 sleep(),则线程进入 WAITING 状态;当线程处理完 run() 方法后,它就进入 DEAD 状态。

package com.yxjc123;
class ThreadDemo implements Runnable {
   private Thread t;
   private String threadName;
   ThreadDemo( String name) {
      threadName = name;
      System.out.println("Thread: " + threadName + ", " + "State: New");
   }
   public void run() {
      System.out.println("Thread: " + threadName + ", " + "State: Running");
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            //让线程休眠一段时间。
            System.out.println("Thread: " + threadName + ", " + "State: Waiting");
            Thread.sleep(50);
         }
      } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
      }
      System.out.println("Thread: " + threadName + ", " + "State: Dead");
   }

   public void start () {
      System.out.println("Thread: " + threadName + ", " + "State: Start");
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
public class TestThread {
   public static void main(String args[]) {
      ThreadDemo thread1 = new ThreadDemo( "Thread-1");
      ThreadDemo thread2 = new ThreadDemo( "Thread-2");
      thread1.start();
      thread2.start();
   }   
} 

输出

Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, State: Waiting
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-2, State: Waiting
Thread: Thread-1, State: Waiting
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Waiting
Thread: Thread-2, 1
Thread: Thread-2, State: Waiting
Thread: Thread-1, 1
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Dead
Thread: Thread-1, State: Dead