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