Java Multithreaded Development Series three: Thread This lifetime (thread life cycle)

Source: Internet
Author: User

The basics of multithreading and the creation of multithreading are already mentioned in the previous article. But if you want to manage multiple threads well, be sure to have a holistic concept of the thread's life cycle. This section describes the lifetime of the thread, giving you a sense of the state of the threads at various times.

The state process of a thread's lifetime

Such as:

          threads can be from birth to run to death. In the previous article (to find the previous article please click here): Java threads are preemptive: The CPU is generally not a thread from the birth of the service to the old, the threads are always scrambling to get the CPU "favor." When a thread is blocked, the CPU is quickly preempted by other threads. The currently blocked thread can only become ready, waiting for the CPU to "favor" the next time. Here's an old saying: opportunities are always reserved for prepared people (currently in readiness), and if you're not ready (to block), chances are fleeting (turn to other threads). so as shown in the following:The thread is new when it is born (after it is new), where the JVM allocates memory, initializes the field, and so on. It then enters the ready state (executing the Start method) and starts to queue, immediately after the CPU starts executing (preemption to the resource) that thread, the thread goes into the running state, and then for some reason waits (such as sleep, waiting for the resource to be called, such as being consumed by other resources, Or turn on the need for preparation time) and be forced into a blocking state . After the wait is over (after the sleep time, waiting for the resource to be freed) the thread will be ready again, waiting for the CPU to look back. At the same time, if the running state of the thread due to the problem of CPU scheduling, and the loss of operational rights will be turned into a ready state , waiting for the advent of the CPU. When a running thread finishes executing a task in the thread body, or hangs due to an exception, the thread enters the dead state . Status Jump Detail:in the case of each step (en route number) represented
Create new----> Ready runnable Run the start () method
Ii Ready runnable----> Run running Get CPU Resources
run running----> block blocked

suspended,

Wait for other resources,

wait for a notification (described later),

wait for an IO resource

Blocking blocked----> Ready runnable

Time to Sleep,

Suspended after being restored,

Waiting for resources to be released,

Get notifications to other threads (described later),

Io method has returned

Run running----> Ready runnable

The processor discards the current thread and handles other threads.

Run running----> Death dead

The executing body of the current thread has been run over,

The current thread has thrown an exception or error that cannot be resolved

Some other areas to note:

(1) When the thread starts, it will not run immediately, depending on the schedule of the virtual machine (mentioned above)

(2) Once a thread dies, it can no longer be ready to be restarted (using the Start method), and if forced start throws an exception.

(3) To determine whether the current thread has been started and is not dead (ready, running, blocking), you can use the IsAlive () method to determine

(4) The boot thread is ready to start with the start () method, not the Run method, because it was also mentioned when creating multiple threads.

(5) Once a thread is started, everyone is handled independently, and it is not affected by the state of the parent thread (the thread that created and started the child thread), which affects itself (such as when the parent thread dies, the child thread continues to run).

Java Multithreaded Development Series three: Thread This lifetime (thread life cycle)

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.