The state of the Java multi-thread threads and the state transformations caused by collaborative communication between threads

Source: Internet
Author: User

One: The state of the thread and the change diagram

There are five types of threads in Java: New (new state), Runnable (Ready state), Running (running state), Blocked (blocking state), Dead (dead State).

NEW: The new state, when the thread creation is complete, is new, that is, new thread (...), and the Start method is not yet called when the thread is in the new state.

Runnable: The ready state, when the thread's Start method is called, the threads go into a ready state, waiting for CPU resources. A thread that is in a ready state is dispatched by the thread scheduler of the Java Runtime System (threadsScheduler).

Running: The running state, the ready state of the thread gets into the running state after the CPU execution, starts executing the Run method.

Blocked: Blocking state, the thread is not finished, for some reason (for example, I/O operations, etc.) to give up the CPU execution, itself into the blocking state.

Dead: The state of death, when a thread executes or an exception occurs during execution, the thread enters a dead state.

(Figure from the cattle network, if there is an offence, please inform the deletion.) )

Two: Inter-thread collaboration communication and its resulting state changes

1: Synchronous Collaboration between threads

Synchronization between threads is realized by syncrhoized synchronous lock, Reentrantlock, Readwritelock waiting synchronization mechanism.

The thread obtains the lock to enter the ready state, waits for the CPU to dispatch enters the running state;

When a thread requests a lock that is occupied, it goes into a blocking state and yields the CPU usage. Until the lock is acquired, re-enter the ready state, waiting for the CPU to dispatch into the running state.

2: Communication Collaboration between threads

When the thread executing the lock is executed, the other threads executing to the same lock will need to be executed first, and then the synchronization lock and the CPU (into the blocking state) will need to be executed before the other thread acquires the synchronization lock and the CPU. Until the other thread finishes and releases the sync lock, notifies it to wake (ready), then requests the sync lock and the CPU to continue execution (run state).

The thread's communication is the way to make resources, suspend, or wake up between the threads.

syncrhoized lock thread of Wait ()/notify ()/notifyall () and Reentrantlock lock thread condition call await ()/signal ()/signalall () is the specific thread communication.

3: The action of the thread itself

The thread itself can enter the blocking state by calling the sleep () method, temporarily yielding the CPU resources (but not releasing the lock), and then automatically recovering the ready state after the sleep time waits for the CPU to dispatch the execution;

The thread itself can be changed from run state to ready state by calling the yield () method, which is called "concession", that is, the running thread gives up the CPU to the ready state of the thread to execute first, and then returns to the ready state to wait for the CPU to dispatch itself to execute;

A thread can pass through other thread objects during its own execution. The join () method executes the other threads that have already been started , and then proceeds with the rest of its operations. This method can be used to implement sequential execution between threads.

The state of the Java multi-thread threads and the state transformations caused by collaborative communication between threads

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.