Java的六种线程状态及代码示例
Java的线程有6个状态,分别是
State | 状态 | 解释 | 时机 |
NEW | 新建 | 新建出来的线程 |
执行Thread的start方法前 |
RUNNABLE | 运行 | 可以细分为运行中和就绪,正在运行或者等待CPU调度。 | 执行Thread的start方法后。yield后。Object.notify、Object.notifyAll、LockSupport.unpark后 |
BLOCKED | 阻塞 | 还未竞争到的锁的阻塞在那的情况,使用synchronize,但是还没竞争到锁 | 阻塞在synchronize处 |
WAITING | 等待 | 让出CPU,等待被唤醒 | 调用wait、join、park方法后 |
TIMED_WAITING | 计时等待 | 让出CPU,等待被唤醒 或者 时间到了自动唤醒 | 调用Thread.sleep(long)、Object.wait(long)、join(long)、LockSupport.parkNanos(long)、LockSupport.parkUntil(long)方法后 |
TERMINATED | 终止 | 线程的任务执行完成 | 正常执行完成run代码 |
操作系统的线程其实是五种状态,和Java的线程六种状态有些许区别,如下是操作系统的五种线程状态
new 新建状态
runnable 就绪状态
runnning 运行状态
block 阻塞状态,又可以细分为等待阻塞、同步阻塞、其他阻塞,可以大概对应到java中的BLOCKED、WAITING、TIMED_WAITING
dead 死亡状态,就是java中的TERMINATED
如下是Java中线程各个状态的转换图
可以从源码看到这些枚举值
如下是为了加深理解,模拟出的线程的各个状态
public class ThreadStateTest { public static void main(String[] args) throws InterruptedException { NEW_RUNNABLE_TERMINATED(); BLOCKED_by_synchronized(); WAITING_by_wait(); WAITING_by_join(); WAITING_by_park(); TIMED_WAITING_by_wait_timeout(); TIMED_WAITING_by_sleep(); TIMED_WAITING_by_join_timeout(); TIMED_WAITING_by_parkUntil(); TIMED_WAITING_by_parkNanos(); } private static void NEW_RUNNABLE_TERMINATED() throws InterruptedException { Thread t1 = new Thread(() -> { while(!Thread.currentThread().isInterrupted()) { for(long j=0;j<100;j++) {} } }); System.out.println("state:" + t1.getState() + " after new"); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " after start"); t1.interrupt(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " after all"); } private static void BLOCKED_by_synchronized() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { } }); synchronized (o1) { t1.start(); Thread.sleep(1000); System.out.println("state:" + t1.getState() + " BLOCKED_by_synchronized"); } } private static void WAITING_by_wait() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { try { o1.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); Thread.sleep(1000); System.out.println("state:" + t1.getState() + " WAITING_by_wait"); synchronized (o1) { o1.notifyAll(); } } private static void TIMED_WAITING_by_wait_timeout() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { try { o1.wait(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_wait_timeout"); } private static void WAITING_by_join() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); Thread t2 = new Thread(() -> { t1.start(); try { t1.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } }); t2.start(); Thread.sleep(500); System.out.println("state:" + t2.getState() + " WAITING_by_join"); } private static void WAITING_by_park() throws InterruptedException { Thread t1 = new Thread(() -> { LockSupport.park(); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " WAITING_by_park"); Thread.sleep(1000); LockSupport.unpark(t1); } private static void TIMED_WAITING_by_sleep() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_sleep"); } private static void TIMED_WAITING_by_join_timeout() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(4 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); Thread t2 = new Thread(() -> { t1.start(); try { t1.join(2 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); t2.start(); Thread.sleep(500); System.out.println("state:" + t2.getState() + " TIMED_WAITING_by_join_timeout"); } private static void TIMED_WAITING_by_parkUntil() throws InterruptedException { Thread t1 = new Thread(() -> { long nowMills = System.currentTimeMillis(); LockSupport.parkUntil(2*1000+nowMills); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_parkUntil"); } private static void TIMED_WAITING_by_parkNanos() throws InterruptedException { Thread t1 = new Thread(() -> { LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2)); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_parkNanos"); } }
wait、join、park