-
NEW : 线程刚刚创建,还没有执行start启动
-
RUNNABLE : 可运行状态,由线程调度器可以安排执行
-
包括READY和RUNNING两种细分状态
-
-
WAITING: 等待被唤醒
-
TIMED WAITING: 隔一段时间后自动唤醒
-
BLOCKED: 严重被阻塞,正在等待锁(只有synchronized(操作系统调度)才会出现)
-
TERMINATED: 线程结束
public static void main(String[] args) throws InterruptedException { //t1 Thread t1 = new Thread(()->{ //线程在执行runnable状态 System.out.println("2: "+Thread.currentThread().getState()); for (int i=0;i<3;i++){ SleepHelper.sleepSeconds(1); System.out.println(i + " "); } System.out.println(); }); //线程new了,但是还没start所以是NEW System.out.println("1: "+t1.getState()); t1.start(); t1.join();//等地t1线程结束. //线程结束 terminated System.out.println("3: "+t1.getState()); //t2 Thread t2 = new Thread(()->{ LockSupport.park(); //线程等待被叫醒,waiting System.out.println("t2 go on"); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } }); t2.start(); TimeUnit.SECONDS.sleep(1); System.out.println("4: "+t2.getState()); LockSupport.unpark(t2); //叫醒t2线程. TimeUnit.SECONDS.sleep(1); System.out.println("5: "+t2.getState());//因为sleep 5m,所以timed_waiting //t3 final Object o = new Object(); Thread t3 = new Thread(()->{ synchronized (o){ System.out.println("t3 得到了锁 o"); } }); new Thread(()->{ synchronized (o){ SleepHelper.sleepSeconds(5); }; }).start(); SleepHelper.sleepSeconds(1); t3.start(); SleepHelper.sleepSeconds(1); System.out.println("6: " + t3.getState()); //只有synchronized才会进入 block 状态 //t04 final Lock lock = new ReentrantLock(); Thread t4 = new Thread(()->{ lock.lock(); System.out.println("t4 得到了锁 lock"); lock.unlock(); }); new Thread(()->{ lock.lock(); SleepHelper.sleepSeconds(5); lock.unlock(); }).start(); SleepHelper.sleepSeconds(1); t4.start(); SleepHelper.sleepSeconds(1); System.out.println("7: "+t4.getState()); //t5 Thread t5 = new Thread(()->{ LockSupport.park(); }); t5.start(); SleepHelper.sleepSeconds(1); System.out.println("8: "+t5.getState()); LockSupport.unpark(t5); }
结果:
"D:\Program Files\Java\jdk1.8.0_271\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.1\lib\idea_rt.jar=51575:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_271\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_271\jre\lib\rt.jar;E:\IdeaProject\lambda\target\classes" com.cebbank.juc.c_000_threadbasic.T04_ThreadState 1: NEW 2: RUNNABLE 0 1 2 3: TERMINATED 4: WAITING t2 go on 5: TIMED_WAITING 6: BLOCKED 7: WAITING t3 得到了锁 o 8: WAITING t4 得到了锁 lock Process finished with exit code 0
标签:Files,状态,jdk1.8,java,jar,271,Program,线程,Java From: https://www.cnblogs.com/belen87/p/16939482.html