Java multithreaded (eight) thread state diagram

Source: Internet
Author: User


Combined with the multi-threaded learning process, the state diagram of the thread is introduced, and as the study progresses, the image is constantly added to the new content.

I. Thread basic state diagram

This picture is in the Java Multithreading (three) thread life cycle and priority has occurred:

The diagram is the basic state of the thread's operation: when the thread calls the start () method, it enters the operational state, switches between running and running as the CPU's resource is scheduled, and then goes into a blocking state when it encounters blocking.

two. Join the synchronized thread state diagram

Multithreading synchronization mechanism, and the use of synchronized keyword learning:

Java Multithreading (v) multi-threaded synchronization

Java Multithreading (vi) Synchronized keywords

The state diagram then joins the bottom part, as follows:

  

When the resource is accessed by one thread, the other thread enters a lock pool;

When the lock is released, the other thread gets the lock and becomes operational.

three. Join the interaction between threads

Interaction between Threads: Java multithreading (vii) communication between threads

This is mainly about the wait and notify method.

A thread state diagram with Wait () and notify ():

  


After the thread calls the Wait () method, it releases the lock, enters the wait pool , waits for the lock after receiving the notification, and then runs after the lock is acquired.

Summary

  thread blocking may be due to the following five reasons : ("Thinking in Java")

1. Call sleep (in milliseconds) to put the thread into sleep. This thread will not run for a specified period of time.

2. Suspend execution of the thread with suspend (). The "operational" status is not returned unless a resume () message is received.

3. Suspend execution of the thread with Wait (). Unless the thread receives a notify () or notifyall () message, it does not become a "operational" state.

4. The thread is waiting for some IO operations to complete.

5. The thread tries to invoke the "synchronous" method of another object, but that object is locked and temporarily unusable.

So you can still use the first graph to generalize about the thread's life cycle and its various states. However, there are many types of threads that are blocked, and the following figure is a refinement description.

  The two types of pools involved are:

Wait Pool

Lock Pool

Java multithreaded (eight) thread state diagram

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.