Java Thread状态
NEW - 创建
被创建,但是还没有开始的线程,也就是还没有调用start()方法。
RUNNABLE - 运行就绪
可运行状态,在JVM中执行,但是可能等待操作系统CPU资源。
BLOCKED - 阻塞
线程阻塞等待获取监视器锁。 两种情况:
- 等待监视器锁以进入同步块/方法;
- 在调用完Object.wait方法后被唤醒,等待重新进入同步块/方法;
WAITING - 等待
等待状态,调用如下方法会导致进入等待状态:
- Object.wait ,无超时时间参数;
- Thread.join, 无超时时间参数;
- LockSupport.park
用于一个线程等待另外的线程去执行某些特殊动作。例如:一个线程调用Object.wait, 等待其他线程调用Object.notify() 或 Object.notifyAll() 来唤醒。一个线程调用Thread.join() 等待另一个线程终止。
TIMED_WAITING - 定时等待
带有一个指定的等待时间的等待状态,带一个指定的正的等待时间调用如下方法可导致进入定时等待状态:
- Thread.sleep
- Object.wait, 带超时时间参数
- Thread.join, 带超时时间参数
- LockSupport.parkNanos
- LockSupport.parkUntil
TERMINATED - 终止
终止状态,线程执行完成
线程状态程序演示
代码
1public class TestThread implements Runnable { 2 3 public static void main(String[] args) throws InterruptedException { 4 TestThread tt = new TestThread(); 5 Thread testThread = new Thread(tt); 6 7 // 1. NEW - 创建testThread线程,未start 8 System.out.printf("1. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); 9 testThread.start(); 10 11 Thread.sleep(1000L); 12 13 // 3. TIMED_WAITING - testThread线程内部sleep 2秒 14 System.out.printf("3. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); 15 16 tt.testBlock(testThread); 17 18 Thread.sleep(2000L); 19 20 // 6. WAITING - testThread线程中调用了tt.wait(); 21 System.out.printf("6. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); 22 23 tt.testNotify(testThread); 24 25 Thread.sleep(2000L); 26 27 // 9. TERMINATED - testThread执行完成,终止 28 System.out.printf("9. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); 29 } 30 31 @Override 32 public void run() { 33 Thread curThd = Thread.currentThread(); 34 long tid = curThd.getId(); 35 36 // 2. RUNNABLE - 开始执行 37 System.out.printf("2. ENTER: %d - %s\n", tid, curThd.getState().toString()); 38 39 try { 40 Thread.sleep(2000L); 41 } catch (InterruptedException e) { 42 e.printStackTrace(); 43 } 44 45 synchronized (this) { 46 47 // 5. RUNNABLE - 获取当前对象监视器锁 48 System.out.printf("5. enter sync block: %d - %s\n", tid, curThd.getState().toString()); 49 50 try { 51 wait(); 52 } catch (InterruptedException e) { 53 e.printStackTrace(); 54 } 55 56 // 8. RUNNABLE - testThread获取到监视器锁,继续执行 57 System.out.printf("8. end sync block: %d - %s\n", tid, curThd.getState().toString()); 58 } 59 } 60 61 public synchronized void testBlock(Thread testThread) { 62 63 try { 64 Thread.sleep(3000L); 65 } catch (InterruptedException e) { 66 e.printStackTrace(); 67 } 68 69 // 4. BLOCKED - 当前线程获取了当前对象的监视器锁,testThread等待监视器锁中; 70 System.out.printf("4. testBlock: %d - %s\n", testThread.getId(), testThread.getState().toString()); 71 } 72 73 public synchronized void testNotify(Thread testThread) throws InterruptedException { 74 notify(); 75 76 try { 77 Thread.sleep(1000L); 78 } catch (InterruptedException e) { 79 e.printStackTrace(); 80 } 81 82 // 7. BLOCKED - 调用了tt对象的notify方法,testThread的状态由WAITING变为BLOCKED, 之所以是BLOCKED,因为tt对象的监视器锁被当前线程获取。 83 System.out.printf("7. testNotify: %d - %s\n", testThread.getId(), testThread.getState().toString()); 84 } 85} 86
输出
11. MAIN: 11 - NEW 22. ENTER: 11 - RUNNABLE 33. MAIN: 11 - TIMED_WAITING 44. testBlock: 11 - BLOCKED 55. enter sync block: 11 - RUNNABLE 66. MAIN: 11 - WAITING 77. testNotify: 11 - BLOCKED 88. end sync block: 11 - RUNNABLE 99. MAIN: 11 - TERMINATED