Thread wake-up multi-thread wait wake-up mechanism thread wake-up

Source: Internet
Author: User

A complaint has nothing to do with the subject:

To sum up the importance of multithreading-waiting for the wake-up Mechanism

Let's use a difficult example to explain the logic of waiting to wake up the machine. When we were young, we had a game and couldn't remember the name. We were a group of friends, one of the lucky ones is drawn out, and the others are running around and escaping from the lucky one. Rule: When the lucky one wants to catch one of the other friends, if the response is fast, say "wood (fixed, cannot be moved)", lucky little pot friends can not catch him, but to catch other, only when other friends come to touch him (the one in the wood) can they be saved. When all the trees are finished, the luckier friends will become lucky, at the same time, it is completely saved.

Analysis points: the best friend is like a CPU and is switching. The friend of wood is like a thread, and "Wood" is like wait (), if you touch him, it's like running y (), and saving the whole department is like running yall (),

The relationships between threads are the same, and there is no attachment between them. They compete for CPU resources and have their own opinions. They also unconditionally obstruct other threads from accessing shared resources. However, there are also many practical problems that require not only the same access requests to share resources, but also the interaction between threads and push forward through mutual communication.

Note: 1. In the loop type selection, it is best to use the while loop, which must be judged first,

2. Wake up with policyall () when multiple threads exist ();

Simple wait wake-up mechanism code generation:


The article ends with sharing some jokes and quotations from programmers:

View of love
Love is an endless loop, and once executed, it gets stuck.
Falling in love with a person means memory leakage-you can never release it.
When you really fall in love with a person, it is constant limitation and will never change.
My girlfriend is a private variable. Only this class can be called.
The lover must pay attention when using the pointer. Otherwise, it will bring about a huge disaster.

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.