線程的狀態包含,NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED 線程狀態相關定義由java.lang.Thread.State枚舉給出 /** * Thread state for a thread which has not yet started. */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED;
private static void testThreadNew() { Thread thread = new Thread(); System.out.println(thread.getState()); } Console:NEW 當線程被建立且不作其餘操做時爲NEW
private static void testThreadRunnable() { Thread thread = new Thread(); thread.start(); System.out.println(thread.getState()); } 當前線程被建立且.start時線程進入Runnable狀態
private static final Object lockObj = new Object(); private static void testThreadBlocked() throws InterruptedException { Thread thread1 = new Thread(new TestBkickedRunnable()); thread1.setName("線程1"); thread1.start(); //主線程阻塞,使得線程1 同步住 Thread.sleep(1000L); Thread thread2 = new Thread(new TestBkickedRunnable()); thread2.setName("線程2"); thread2.start(); Thread.sleep(Integer.MAX_VALUE); } static class TestBkickedRunnable implements Runnable { @Override public void run() { synchronized(lockObj) { try { //sleep會持有鎖,wait不會 Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } } } } 線程等待監視鎖阻塞的線程狀態。 處於阻塞狀態的線程正在等待監視器鎖。 當線程1使用同步塊或方法獲取對象監視鎖後未釋放,線程2想獲取監視鎖這個監視鎖時,線程2從新進入同步塊/方法調用後會進入blocking狀態。 查看方式 1.使用 jps -m 獲取到pid 2. 使用stack pid 查看 關注設置的制指定名稱的線程狀態
注意block狀態只針對於synchronized有效,當使用java.util.concurrent.lock時沒有這個狀態java
private static final Object lockObj = new Object(); private static void testThreadWait() { Thread thread = new Thread(new Runnable() { @Override public void run() { try { synchronized (lockObj) { lockObj.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.setName("等待線程"); thread.start(); } 線程調用wait,join,LockSupport.park方法進入等待狀態 當被調用notify,notifyAll,LockSupport.unpark時解除
private static final Object lockObj = new Object(); private static void testThreadWaitTimed() { Thread thread = new Thread(new Runnable() { @Override public void run() { try { synchronized (lockObj) { lockObj.wait(1000000L); } } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.setName("等待超時線程"); thread.start(); } 線程調用wait(time),join(join),LockSupport.parkNanos,LockSupport.parkUntil方法進入等待狀態 達到指定時間時解除
private static void testThreadTerminated() throws InterruptedException { Thread thread = new Thread(); thread.setName("結束線程"); thread.start(); //給線程執行時間 Thread.sleep(200L); System.out.println(thread.getState()); TERMINATED Thread.sleep(Integer.MAX_VALUE); } 線程執行完畢後進入狀態