Java FAQ _01 Basic Concepts (007) _ What are the status of threads

Source: Internet
Author: User
Tags object object

Click to enter _ more _java thousand ask

1. What are the status of threads?

In Java, the Java.lang.Thread class has a variable threadstatus that indicates the current state of the thread, which is an int type, but the corresponding get method return value is an enumeration (the internal class of thread), the source code is as follows:

Public enum State {/** * Thread state forA thread which has notYet started. * * NEW,/** * Thread State forA runnable thread. A threadinch  theRunnable * State isExecutinginch  theJava Virtual Machine but itMay * be waiting forOther resources from  theOperating system * Such asProcessor. */RUNNABLE,/** * Thread State forA thread blocked waiting forA monitor lock. * A Threadinch  theBlocked state isWaiting forA Monitor lock * toEnter a synchronized Block/methodor* Reenter a synchronized Block/method AfterCalling * {@link Object#wait () object.wait}.*/BLOCKED,/** * Thread State forA waiting thread. * A Thread is inch  theWaiting state Due toCalling 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 threadinch  theWaiting state isWaiting forAnother thread to* Perform a particular action. * For example, a thread thathas called <tt>object.wait () </tt> * onAn object isWaiting forAnother thread toCall * <tt>object.notify () </tt>or<tt>object.notifyall () </tt> on* thatObject. A thread thathas called <tt>thread.join () </tt> * isWaiting forA specified thread toTerminate. */Waiting,/** * Thread State forA waiting thread withA specified waiting Time. * A Thread is inch  theTimed Waiting state due toCalling one of* theFollowing methods withA 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 forA terminated thread.         * The thread has completed execution.    */TERMINATED; }

As you can see, the thread contains 6 visible states: NEW, RUNNABLE, BLOCKED, waiting, timed_waiting, TERMINATED.
We can also see through JAVAVISUALVM's thread monitoring that it monitors the running of threads (corresponding to runnable), hibernation (corresponding to new), Waiting (waiting+timed_waiting), residency (BLOCKED), monitoring status, Completed (TERMINATED) six states.

Learn about monitoring the JVM look here: How to monitor the operation of the JVM

For the State enumeration, the source code comments are written very clearly, here we specifically look at:

NEW
A thread that has not yet started are in this state.
A thread that has not yet started is in this state.

Indicates that the thread has not started.

RUNNABLE
A thread executing in the Java virtual machine was in this state.
A thread that executes in the JVM is in this state.

can run threads. A thread in the Java Virtual machine is in a running state, that is, it is waiting for other resources or operating system processing, once the CPU resources are obtained to execute, then into the people said the implicit running state (this state JVM does not care, we are not particularly concerned about, The General JVM monitoring tool does not count this state).

BLOCKED
A thread that's blocked waiting for a monitor lock are in this state.
A blocked thread is in this state and it is waiting for the monitor lock.
For some reason, such as waiting for user input and so on, let the current CPU to other threads execute, will enter the blocked state.
A thread in the blocked state waits for the monitor lock and then executes the synchronized code block/method. Or, after calling Object.wait (), execute the Synchronized code block/method.

Waiting
A thread that's waiting indefinitely for another the thread to perform a particular action was in the this state.
One thread is waiting indefinitely for another thread to wake up in this state.

Enter the waiting state using the following methods:

    1. Call Object.wait () and no timeout
    2. Call Thread.Join () and no timeout
    3. Call Locksupport.park (Object)

One thread in the waiting state needs to be activated by another thread. For example, one thread executes object.wait () and waits for another thread to call the object Object.notify () or Object.notifyall ().

One thread invokes the Thread.Join () method of another thread, and the execution continues after another thread executes (the Join method can specify a deferred execution time).

timed_waiting
A thread that's waiting for another the thread to perform an action for up to a specified waiting time was in this state.
One thread waits for another thread to wake up within a time threshold, and the threshold is returned to the runnable state.

Enter the Timed_waiting state using the following methods:

    1. Call Thread.Sleep ()
    2. Call Object.wait () exceeds the time threshold value
    3. Call Thread.Join () exceeds the time threshold value
    4. Call Locksupport.parknanos (Object,long)
    5. Call Locksupport.parkuntil (Object,long)

TERMINATED
A thread that has exited is in the this state.
A thread exits in this state.

The completion of the thread execution becomes this state.

Java FAQ _01 Basic Concepts (007) _ What are the status of threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.