The life cycle of a Java thread

Source: Internet
Author: User

The life cycle of a Java thread

A thread is generated from the Start method we call to enter the runnable state, that can be scheduled to run state, and does not really start to run, the scheduler can allocate the CPU to it, so that the thread into the running state, really run the program code. While the thread is running, there are a couple of possible whereabouts:

(1) When the scheduler allocates the CPU to other threads during the execution of a thread, the thread becomes runnable state and waits to be dispatched.

(2) The dispatcher assigns the CPU to the thread, does not encounter any blocking during execution, and the run completes directly, that is, the run () method executes.

(3) The thread requests the lock in the execution process, but does not get it, when it wants to enter lock pool waits for the object's locks, waits for the lock to enter the runnable state again, can be dispatched to run.

(4) When the thread encounters the Wait () method during execution, it waits in the wait pool until it is executed by the Notify () or interrupt () method before it is awakened or interrupted into the lock pool and waits for the object lock until the lock enters the runnable state.

It is recommended that you use the Run method to end a thread in a way that controls the loop condition.

wait: tells the current thread to discard the object lock and enter the wait state until another thread enters the same object lock and calls notify.

Notify: Wakes the first thread of a call to wait in the same object lock.

Notifyall: Wakes all threads that call wait in the same object lock, and the thread with the highest priority is first awakened and executed.

Yield: If you know that you have completed the work required during the iteration of the Run () method, you can give the thread a mechanism to dispatch a hint: my work has been done almost as much as possible for other threads to use the CPU. Implemented by calling yield ().
When yield is called, you are also recommending that other threads with the same priority be able to run.
For any important control or adjustment of the application, it is not the one that relies on yield. In fact, yield is often misused.
(yield does not mean quitting and pausing, just telling the thread to dispatch if someone needs to, can take it first, I'll do it again, no one needs, I continue to do)
The lock was not released when yield was called.

Interrupt Brief Introduction

The interrupt () method simply alters the interrupt state, and it does not break a running thread. The actual completion of this method is to send an interrupt signal to the blocked thread so that the blocked thread can exit the blocked state. Rather, if the thread is blocked by one of the three methods of Object.wait, Thread.Join and Thread.Sleep, then the interrupt () method of the thread is called, then the thread throws a Interruptedexception Interrupt exception (the thread must be prepared to handle this exception beforehand), thus terminating the blocked state early. If the thread is not blocked, calling interrupt () will not work until it is executed to wait (), sleep (), join (), and the interruptedexception is immediately thrown.

When thread a executes sleep,wait,join, thread B calls thread A's interrupt method, and indeed at this point a will have a interruptedexception exception thrown out. But this is actually in sleep,wait,join these methods inside will constantly check the value of the interrupt state, while the interruptedexception itself throws. If thread A is performing some of the specified operations, such as values, For,while,if, calling methods, and so on, does not check the interrupt state, thread A does not throw interruptedexception, and will always perform its own operation.

Note :

When thread a executes to wait (), sleep (), join (), when Interruptedexception is thrown, the interrupt state has been reset by the system, and thread A calls thread.interrupted () returns false.

If the thread is called interrupt (), the thread is not in wait (), sleep (), join (), and the next time you execute Wait (), sleep (), join () will throw interruptedexception, Of course, the interrupt state of the thread after it is thrown is also reset by the system.

1. Sleep () &interrupt ()

Thread A is using sleep () to pause: Thread.Sleep (100000), if you want to cancel its wait state, you can call A.interrupt () [A is the thread instance that threads a corresponds to] in the executing line thread (for example, this is B). Cause thread A to abandon the sleep operation. That is, thread B is executing a.interrupt (), and threads a in blocking will abandon the sleep operation.

When the thread is called interrupt () in sleep, it immediately discards the paused state and throws interruptedexception. The exception that is thrown is a thread.

2. Wait () &interrupt ()

Thread A calls wait () and can be canceled with interrupt (). However, you should pay attention to the problem of locking. The thread enters the waiting area, unlocks the lock, and when it calls interrupt () on the waiting thread, it acquires the lock again and throws the exception. It is not possible to throw an exception until the lock is acquired.

3. Join () &interrupt ()

When a thread waits for another thread to end with a join (), when it is called interrupt (), it, like sleep (), jumps to the catch block immediately.

Note that the interrupt () method that is called must be the interrupt method that calls the blocked thread. such as thread T.interrupt () is called in threads A.

Http://www.open-open.com/lib/view/open1417421714612.html

Java Thread life cycle (RPM)

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.