4 states of Java threads and Transformation Analysis __java

Source: Internet
Author: User

New-start ()->

Runnable-wait (), sleep (), yield ()->

<-Notify ()/notifyall ()/timeout

Not Runnable (Wait/sleep/block)-Exit run ()->

Death

After a thread is created, it is always in one of the 4 states of its life cycle. The state of the thread indicates the activity currently being performed by this thread, and the state of the thread can be controlled by the program, that is, the thread can be manipulated to change the state. These actions include startup (start), termination (stop), sleep, suspend (suspend), recovery (resume), Waiting (wait), and notification (notify). Each operation corresponds to a method that is provided by the package Java.lang.
① Create (new) status
If you create a thread without starting it, the thread is in the created state. For example, after executing the following statement, the system has a thread mythread that is in the created state:
Thread mythread= new Mythreadclass ();
where Mythreadclass () is a subclass of thread, and thread is provided by the Java.lang package of the Java system.
The thread that is in the created state has not been given the resources it deserves, so this is an empty thread. The system will not allocate resources to a thread until it is started. There are two things you can do with a thread that is in the state of creation: one is to start the operation, make it into a running state, and the other is to terminate (stop) the operation and make it go extinct. If you go into extinction, then the thread cannot enter another state, that is, it no longer exists.
The Start method is a method that corresponds to the startup action, which is to allocate the necessary system resources to the thread, and to set the thread to run so that the system can dispatch the thread.
② Run (runnable) status
If you start an operation on a thread that is in the Create state, the thread enters a running state. Still, take the mythread thread you created earlier with the following statement: Mythread.start (); The thread mythread into a running state. The above statement essentially invokes the thread body, the run () method. Note that the run () method is contained in the Mythread thread, which is first passed the run () method to the subclass Mythreadclass () by the thread class of the Java.lang package, and then passed to the thread by creating the thread because of the class Mythreadclass () Mythread.
A thread in a running state only indicates that it has a running condition, but the running state is not necessarily a running state. Because running multithreaded programs in a single-processor system, there is actually only one thread running at a point in time, and there are often multiple threads running in the system at the same time. The system allows all the running threads to share the processor through fast switching and scheduling, resulting in a macroscopic multithread concurrency operation.
Visible, whether a thread is in the running state, except that it must be in a running state, depends on the scheduling of the system.
In a running state, you can do a variety of things, most commonly from the run () method to exit normally and end the thread, into extinction. In addition, you can also do the following:
Suspend operation, by calling suspend method to implement;
The sleep operation is realized by calling the sleeping method;
Waits for the operation, by calling the wait method to implement;
The concession operation is realized by invoking the yield method;
Terminates the operation by calling the Stop method.
The previous three operations will cause a thread in a running state to enter a state that is not running. For example, still take the mythread thread as an example, when it is in a running state, and then use the following statement:
Mythreadsleep (5000);
The sleep method is called so that the Mythread thread sleeps for 5 seconds (5000 milliseconds). Within 5 seconds, this thread cannot be scheduled to run by the system, and only after 5 seconds will the Mythread thread wake up and automatically return to the operational state.
If a thread is taken to a non-running state by a pending operation, then the resume must be invoked to make the thread return to its operational state.
A concession is another thread that enables a thread to forward CPU control to a peer priority.
A thread that can run state can also make it extinct by calling the Stop method.
③ not run (not runnable) status
Non-running states are transformed by a running state. A thread that is in a running state, if it encounters a suspend (suspend) operation, sleep operation, or waits (wait) operation, will enter a state that is not operational. In addition, if a thread is associated with I/O operations, the thread is not automatically returned to the operational state when the I/O instruction is executed, because the peripheral speed is much lower than the processor speed and the threads are blocked, thereby entering the running state, only after the peripheral completes the input/output. Once a thread has entered a state that is not operational, it can return to a running state. There are usually three ways to get it back to a running state.
One is automatic recovery.
A thread that enters an inactive state through a sleep operation is automatically restored to a running state after a specified sleep time, and a thread that enters an inactive state due to I/O blocking automatically reverts to the operational state after the peripheral completes I/O.
The second is to restore it by means of a recovery (resume) method.
If a thread enters a suspend state from a running state because of a pending (()) operation, the recovery (resume) operation must be used to restore it to a running state.
The third is to use the notification (notify or notiyA11) method to restore it.
If a thread that is in a running state is turned into a non running state because of the wait operation surface, it must be returned to the operational state by calling the Notify method or the Notifyall method. The wait operation is often the result of a thread waiting for a condition variable, and when this condition variable is obtained, the thread can be notify or notifyall to enable it to revert to a running state.
Every way to recover to a running state is targeted and cannot be crossed. For example, a recovery operation is not valid for a thread that enters a state that is not running because of blocking.
In a not-running state, it can also be aborted by terminating (stop) operations.
④ extinction (dead) state
A thread can go extinct by any other state by terminating (stop) operations. Once a thread has gone extinct, it no longer exists, so it is impossible to move to another state.
Typically, when an application is run, the Stop method terminates the thread if the current application is terminated through another external command. However, the most normal, most common way is because the thread in the operational state to complete its own tasks and "die", and thus into extinction, the completion of the task of the action is implemented by the Run method.

Http://hi.baidu.com/jsoftware/blog/item/4a909424335160044d088d0b.html

http://lavasoft.blog.51cto.com/62575/99153

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.