java线程的状态

线程状态

线程的状态包含,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;

具体事例场景

NEW

private static void testThreadNew() {
    Thread thread = new Thread();
    System.out.println(thread.getState());
}
Console:NEW
当线程被建立且不作其余操做时为NEW

RUNNABLE

private static void testThreadRunnable() {
    Thread thread = new Thread();
    thread.start();
    System.out.println(thread.getState());
}
当前线程被建立且.start时线程进入Runnable状态

BLOCKED

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

WAIT

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时解除

TIMED_WAITING

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方法进入等待状态
达到指定时间时解除

TERMINATED

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);
}
线程执行完毕后进入状态

PS

  1. 线程状态在Thread.State只定义了5种(就绪,等待,执行本身脑补吧)
  2. 状态流程图
  • NEW - STAET -- TERMINATED
  • NEW - START -- WAIT -- TERMINATED
  • NEW - START
  • ... 脑部流程,图仍是本身动手画比较好
相关文章
相关标签/搜索