Java线程的状态;WAITING、TIMED_WAITING与BLOCKED的区别;Object.wait()和Thread.sleep()

1. Java线程的状态包括:

NEW、READY、RUNNING、WAITING、TIMED_WAITING、BLOCKED

2. WAITING、TIMED_WAITING与BLOCKED的区别:

(1)WAITING:进入等待状态,方式:wait/join/park方法进入无限等待,通过notify/notifyAll/unpark唤醒;

(2)TIMED_WAITING:与WAITING类似,方式:a. 给定等待时间的wait/join/park方法;b. sleep方法;

(3)BLOCKED:被动进入等待状态,方式:进入Synchronized块;

私以为官方的才是最靠谱的,以下为来自jdk1.8: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;

3. Object.wait()和Thread.sleep()

(1)Object.wait()会释放锁,等到唤醒条件满足之后,线程进入锁池,获取锁之后进入READY;Thread.sleep()不会释放锁;

(2)wait()与wait(0)同义,无限等待;sleep(0)的意思是不等待。

4. wait()需要先获取对象的monitor锁,才能调用,否则报出异常java.lang.IllegalMonitorStateException,常用写法:

        synchronized(obj){
            try {
                obj.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章