Thread状态

2017-04-27  本文已影响0人  丑星星

一、Thread中对状态定义的枚举

    /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    public enum 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;
    }```

###二、状态解释
<p>*NEW*:线程被创建,还没有调用start()方法。
<p>*RUNNABLE*:线程调用start()方法后的状态,这个时候可能还没有真正的运行,有可能还在等待系统资      源,可能还没获得CPU。
<p>*BLOCKED*: 线程处于阻塞状态,等待锁释放。等待进入synchronized方法或者代码块时会进入这种状态。
<p>*WAITING*:等待状态,当线程调用Thread.sleep()方法、或者调用Object.wait()方法、或者有其他线程调用Thread.join方法、或者线程调用LockSupport.park()方法时会处于这种状态。
<p>*TIMED_WAITING*:和*WAITING*状态类似,但是会指定等待的时间。调用Thread.sleep(long)、Object.wait(long)、Thread.join(long)、LockSupport.parkNanos(long)、LockSupport.parkUntil(Object blocker, long deadline)的时候会进入这个状态
<p>*TERMINATED*:进程结束

###三、引申--synchronized和ReentrantLock实现“阻塞”时的线程状态的区别
  小伙伴们去看ReentrantLock的源码就知道,ReentrantLock是通过LockSupport.park实现阻塞的。所以synchronized实现阻塞后,等待的线程状态是*BLOCKED*状态,而通过ReentrantLock实现阻塞后,现成的状态是*WAITING*状态。看下面一个栗子:

class TestObject {
public void testMethod(){
synchronized (TestObject.class){
try {
Thread.sleep(1000000000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class SynchronizedThread extends Thread{
@Override
public void run() {
TestObject to = new TestObject();
to.testMethod();
}
}

public class Test {
public static void main(String[] args) {
SynchronizedThread st1 = new SynchronizedThread();
SynchronizedThread st2 = new SynchronizedThread();
st1.setName("1111111111");
st2.setName("2222222222");
st1.start();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
st2.start();
}
}

运行后通过jstack命令查看线程状态:

![Paste_Image.png](https://img.haomeiwen.com/i5441790/4228e013b01ad156.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到线程命是‘2222222222’的线程状态是*BLOCKED*

修改一下代码:

class TestObject {
ReentrantLock lock;
public TestObject(ReentrantLock lock){
this.lock = lock;
}
public void testMethod(){
lock.lock();
try {
Thread.sleep(1000000000000L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}

class SynchronizedThread extends Thread{
ReentrantLock lock;
public SynchronizedThread(ReentrantLock lock){
this.lock = lock;
}
@Override
public void run() {
TestObject to = new TestObject(lock);
to.testMethod();
}
}

public class Test {
ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
SynchronizedThread st1 = new SynchronizedThread(lock);
SynchronizedThread st2 = new SynchronizedThread(lock);
st1.setName("1111111111");
st2.setName("2222222222");
st1.start();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
st2.start();
}
}

查看线程状态:

![Paste_Image.png](https://img.haomeiwen.com/i5441790/5e92b703476264e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到线程命是‘2222222222’的线程状态是*WAITING*
上一篇 下一篇

猜你喜欢

热点阅读