A picture to read Java multithreading

Source: Internet
Author: User

1, with questions to see the picture

1) Competing objects lock and compete with CPU resources and the competition is awakened

2) in which case the lock is acquired and in which case the lock is released

2, or that picture

3. Detailed illustrations

1) Thread t = new Thread (), initializes a thread, which is actually a normal object, when his state is new

2) T.start (); The thread is in a ready state (operational state), which is always waiting to run, do not underestimate this start, this start determines whether he is a real thread instance, because start prepares the threading environment for it, if you just call the Run method, then this is an ordinary method. At this time of the thread, will compete for CPU resources, so who is competing to the CPU resources, that is, is dispatched scheduler, then he can from the operational state to the real running state.

3) When the thread gets to the CPU resource, the thread is from the operational state to the real running state, which is running, no doubt, he is now running.

4) If this thread is waiting for the customer input learning, that is, IO exception, and so on various blocking events, it is possible to call the sleep and other blocking events, the thread will be from the running state to block state, this state will not happen anything!

5) Once the blocking event is cleared, such as the user has entered the completion, the IO stream has been closed, sleep has timed out and so on, the thread from the blocking state to ready state, again back to the operational state, at any time competing with other threads resources, waiting to run!

6) The running state of the thread may run into a synchronous method or synchronization block, that is, the synchronized tag method or block, this time the thread gets the lock of the object, other threads will not be able to enter the synchronization method, then these can not execute the thread what to do? They're all stuck here, waiting for the lock to be released, competing to lock the resource, because only the thread that owns the lock is eligible to continue down, and these threads are blocked in the lock pool.

7) Once the thread that is blocked in the lock pool competes for the lock (the thread has been run out of the past or the previous thread ran out of an exception, or wait is called), the thread will be released from the blocking state to the ready state, and it is not possible to assume that the thread will execute immediately. You have to think that thread execution is to get CPU resources, if there is no operating system scheduling, they are not qualified to run!

8) The running state of the thread may enter the synchronization method or synchronization block in the run, this time he has the object of the lock, the supremacy, but because the current environment may cause him no need to continue to execute, so he will give up the lock resources for other threads also have the opportunity to continue to execute, So this thread may invoke the wait method of the object inside the synchronized, and once called, the current thread makes a lock resource and enters the wait pool itself until it wakes up by another thread! If it is not awakened, it will always be in the waiting pool, being blocked by threads, so all they want to do is wake up, because only wake up is likely to continue running!

9) Once the thread that is blocked in the wait pool is awakened (perhaps a synchronized thread called notify or notifyall, or an external call interrupt causes an internal exception and acquires a lock), the thread will switch from the waiting pool to the lock pool , continue blocking, so don't assume that threads will continue to run when they wake up, which is not possible, and they also need to compete for lock resources.

10) When a thread runs out of an exception, or if the thread is running out, the thread ends, that is, the extinction period. If you run out of start, you must start with a new new thread. Then there will be a life cycle.

A picture to read Java multithreading

Related Article

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.