Java的线程有6个状态,分别是
NEW 新建状态。刚new出来的thread,还没有调用start,就是这个状态
RUNNABLE 运行状态(分为运行中和就绪)。正在运行,或者等待CPU调度,调用完start,就是这个状态
BLOCKED 阻塞状态。还未竞争到的锁的阻塞在那的情况,使用synchronize,但是还没竞争到锁
WAITING 等待状态。使用wait、join、park都会进入这个状态
TIMED_WAITING 计时等待状态。使用sleep、wait(时间)、join(时间)、parkNanos、parkUntil都会进入这个状态
TERMINATED 终止状态。正常运行完run方法以后
如下是各个状态的转换图
可以从源码看到这些枚举值
如下是为了加深理解,模拟出的线程的各个状态
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"); } }
标签:Java,Thread,示例,InterruptedException,t1,WAITING,线程,new,sleep From: https://www.cnblogs.com/huainanyin/p/18145300