Java Thread State

Source: Internet
Author: User

threads, like humans, have their own lifecycles, a thread's life cycle from creation to completion, a process that may be in a different state at different times, the thread state is the subject of this subsection, and how many states does the thread have? How are the different states transformed?

There is no strict regulation of the status of threads, as long as the state can be correctly represented, 2-5-7-1, one of the state classifications, a thread from creation to death may experience several states, but at any point in time the thread can only be in one state, a total of five states: new, Can be run (runnable), run (running), non-operational (not runnable), Dead (dead). The transformation of the state of a thread can be controlled by some APIs, such as Start, stop, sleep, suspend, resume, wait, notify, and so on (stop, suspend, Methods such as resume have long been deprecated because they are prone to deadlock problems.

Figure 2-5-7-1

L NEW: A thread was created but not started in a new state, that is, using new MyThread () in the program, and the thread instance created is in this state.

L can run (runnable): The created thread instance calls the start () method and enters the operational state, the thread in this state is not said to be in the running state, we learned in the previous section multithreading scheduling policy that Java multithreading is using preemptive scheduling, Each of the running threads gets the CPU time slice, which can be imagined as having a running thread pool, the start () method puts the thread into a pool of running threads, and the CPU executes the threads in the pool by a certain rule.

L Run (running): When the runtime thread gets to the CPU execution time slice goes in the running state.

L Non-operational (notrunnable): A running thread temporarily abandons the use of the CPU for some reason, possibly due to a suspend, sleep, or wait operation, which may cause the thread to temporarily abandon the CPU when performing I/O operations because the external device speed is much lower than the processor speed. It is also possible for a thread to temporarily abandon the CPU if the synchronization lock is first consumed by another thread during the acquisition of the object's synchronization lock.

L Death (dead): The thread executes the task implemented by the run () method, or because an exception causes the exit task, the thread will no longer be converted to another state after it enters the dead state.

Continue to subdivide non-operational (not runnable) states by 2-5-7-2, new, operational, running, dead four states define and transform as in the preceding, focusing on the non-operational state of the extended three states: blocking (blocked), synchronous Lock (Locked), Wait (Waiting).

L Blocking (blocked): Blocking is triggered by a blocking event, the thread is blocked and the CPU is discarded and temporarily stopped running. The normal thread executes the sleep (), join () method, or an I/O request, and the thread will be in a blocked state, and if Sleep () is performed at the end, the wait interrupt timeout for join (), and the I/O request ends, it will return to the executable state and wait for the CPU to be allocated.

L Synchronous Lock (locked): If a thread is ready to invoke a synchronous method, and the corresponding object of the synchronization method is being occupied by another thread, the thread will enter a synchronous lock state. In fact, every object object in Java has a monitor that is responsible for exclusive processing of synchronous domains during concurrency, that is, when a thread calls a synchronous method of an object, the JVM detects if the monitor of the object is already occupied, and if it is not occupied, The thread is given monitor possession and continues to execute the object's synchronization method, otherwise the thread will be thrown into a waiting thread queue until the monitor is released and all waiting threads continue to compete for monitor tenure, After the gain of the monitor possession before entering the executable state waiting for the allocation of the CPU, to be eligible to perform the synchronization method.

L Wait (Waiting): The running thread executes the wait () method and enters the wait state. An object that executes the wait () method also causes the thread to enter the waiting thread queue for the object, and it also releases the object lock, which discards the possession of monitor. A thread in the waiting thread queue is awakened only when the Notify (), Notifyall () method is called on another thread, and the notify is a thread in a random wake-up wait queue, while Nofityall wakes up all the threads in the waiting queue. When all threads are awakened, the object's monitor tenure is contested, the thread acquiring the possession can be converted to an executable state, waiting for the allocated CPU to execute down, and the other threads continuing to wait.

Figure 2-5-7-2

Java Thread State

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.