7 states of threads and mutual conversion

Source: Internet
Author: User

Link to the original article I know: http://webservices.ctocio.com.cn/java/423/9092423.shtml

This article describes seven States of a thread and their conversion methods.

Today, a colleague asked a question about the thread, and suddenly he was a little blind. He only vaguely remembered it.

When I first learned the thread, I remembered these seven States better than my own names.

These seven states are also compiled into a miserable and beautiful love story.

I did not expect that I can only remember

The truth proves that "good memory is not as good as bad writing ".

Remember it now.

Start with the image

  

An explanation is as follows:

1. There are two ways to implement the Thread. One is to inherit the Thread class and the other is to implement the Runnable interface. However, when we create this object, the thread enters the initial state;

2. When the object calls the start () method, it enters the running state;

3. After the instance is running, when the instance is selected by the operating system, the instance enters the running status after obtaining the CPU time slice;

4. The running status is complicated.

4.1 After the run () method or main () method ends, the thread enters the termination state;

4.2 When the thread calls its own sleep () method or the join () method of other threads, it will enter the blocking state (this state stops the current thread, but does not release the resources occupied ). When sleep () ends or join () ends, the thread enters the runable state and continues waiting for the OS to allocate time slices;

4.3 When the thread calls the yield () method, it means to discard the CPU time slice that is currently obtained and return to the runable state, which is in the same competition as other processes, the OS may continue to run the process;

4.4 When the thread is just running (note that it is not running), it is found that the resource to be called is synchroniza (synchronous) and the lock mark cannot be obtained, will immediately enter the lock pool status, waiting to get the lock mark (in this case, other threads may already be waiting to get the lock mark, then they are in the queue status, first come, first served ), once the thread obtains the lock tag, it is transferred to the operational status, waiting for the OS to allocate CPU time slices;

4.5 When the thread calls the wait () method, it enters the waiting queue (entering this status will release all the resources occupied, different from the blocking Status). After entering this status, automatic wake-up is not allowed. You must rely on other threads to call the Y () or notifyAll () method to wake up (because notify () is only to wake up a thread, however, we cannot determine which thread to wake up. The thread we need to wake up may not be able to wake up. Therefore, we generally use the yyall () method in actual use, when a thread is awakened, it enters the lock pool and waits for the lock mark to be obtained.

After all, I recall that JDK has improved the API usage and greatly improved the efficiency. However, the principle of several state transitions remains the same.

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.