Multithreading (2) -- debut of the waiter (delegate)

Source: Internet
Author: User

This blog originally wanted to explain the delegation directly, but he was afraid that it would be too out of touch with the first article (multithreading (1)-understanding what multithreading is in life, so let the delegate act as a waiter.

In. net, the thread can only be implemented by calling the delegate. The role of the delegate is described below.

The first blog briefly introduced the ing of multithreading in life. Two waiters A and B respectively represent two different threads used to interact with customer a and Customer B. Have you ever considered: what happened when two waiters A and B ordered food for customers A and B to the delicious food on the table?

At this time, the role of the waiter should work:

As shown in, to enable the waiter to notify the cook of the dishes and finally put the dishes on the customer's desk, the waiter needs to contact "The waiter (equivalent. A delegate variable in. Net, which stores the address of the method. As we all know, there are many waiters in the restaurant, assume that there is only one waiter for the same responsibilities (1 # waiter for heat transfer plain food, 2 waiter for heat transfer Leek, 3 # waiter for heat transfer plain food, 4 # waiter for heat transfer plain food ......), Then, the waiter can decide the contact with the waiter based on the customer's food (the specific waiter here represents the method stored in the delegate ).

The example shows that the waiters contacted by the waiters include 1 # And 3 # waiters.

How did the waiter get in touch with the waiter? At this time, the waiter should play a role in recording the menu of the food ordered by the customer. This is the contact information between the waiter and the waiter. After receiving the menu from the waiter, the waiter assigns tasks to different waiters based on different dishes. In this way, the waiter's work style can be hidden from the waiter (that is, the waiter's work content does not need to be disclosed to the waiter, and the waiter only needs the final result ), the final result is that the waiter can bring the food ordered by the customer to the table.

In. the same applies to threads in. net. When a thread Instance Object (a and B represent two thread instance objects) is created, a delegate (a delegate is a set that stores many method pointers) needs to be called, A list of method pointers that can store method signatures in a certain format. The delegate variable can point to a method or append multiple methods, the functions of each method are independent and encapsulated (as if the waiter cannot handle the tasks ). In this way, the delegate variable is passed in the thread, and the delegate variable points to multiple methods, and finally relies on methods to complete specific functions. The delegate variable here represents the "waiter set" in the previous example (each waiter code that meets the same requirements and has different responsibilities ), each waiter in a specific role represents a method that complies with the delegate signature.

In this example, the contact between the waiter and the waiter needs to use the menu, that is. net defines the delegate method signature must have parameters, and the menu is this parameter, used to pass data.

 

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.