. Net Multithreading

Source: Internet
Author: User
Summary after reading: 1. Why is there an appdemain concept? We all know that every process is isolated from each other and communication between processes is very troublesome. To solve the communication problem between processes,. NET introduces a new concept. As a result, multiple. Net processes can run in the same application domain, so that different application domains can be isolated from each other, ensuring security and reducing the difficulty of inter-process communication. 2. Why multithreading? If there is only one CPU, we know that multithreading also has a CPU to execute, and only one thread can be executed at a time. By switching the time slice, the thread is executed in turn based on the thread priority. However, switching time slice also consumes resources. So why do I use multiple threads? In fact, in some cases, multiple threads can provide system performance. For example, when the system requests large data volumes, multiple threads are used to deliver data output to asynchronous threads so that the main thread can maintain its stability to handle other problems. 3. What are the effects of foreground and background threads on the program? The thread started by thread. Start () is the foreground thread by default, and the application domain will be automatically uninstalled only after all foreground threads have finished running. Thread thread has an isbackground attribute. By setting this attribute to true, you can set the thread to a background thread! At this time, the application domain will be uninstalled when the main thread is completed, instead of waiting for the asynchronous thread to run. At that time, if the main thread waits for its background thread to finish and then end, this will use thread. Join () 4. How does the CLR thread pool work? The CLR thread pool does not create a thread immediately during CLR initialization. Instead, the thread pool initializes a thread only when the application creates a thread to execute the task. The initialization of a thread is the same as that of other threads. After the task is completed, the thread will not be destroyed by itself, but will be returned to the thread pool in the suspended state. Until the application sends a request to the thread pool again, the suspended thread in the thread pool activates the execution task again. This not only saves the performance consumption caused by the establishment of threads, but also allows multiple tasks to reuse the same thread repeatedly, thus saving a lot of overhead during the lifetime of the application. 5. when threadpool is used. when queueuserworkitem (waitcallback, object) calls multiple threads, we can see that queueuserworkitem can only pass one parameter without returning the value. How can we implement asynchronous calls with multiple parameters and returned values? You can use delegate to Implement Asynchronous calls with multiple parameters and return values. 6. When delegate is used to implement thread Asynchronization, how can I monitor that multiple running objects have been completed? The iasyncresult. waithandle. waitone function can be used to monitor an object. However, when multiple running objects are monitored, waithandle has two other static methods: waitany (waithandle [],
INT) and waitall (waithandle [], INT ). 7. How to Use delegate to complete asynchronous calls? First, use round-robin to determine whether the asynchronous process is complete. For example, // set up the delegate mydelegate = new mydelegate (Hello); // call the delegate asynchronously to obtain the computing result iasyncresult result = mydelegate. begininvoke ("Leslie", null, null); // execute other work before the asynchronous thread is completed while (! Result. iscompleted) {thread. Sleep (200); // The Master thread operates console. writeline ("Main thead do work! ");} String data = mydelegate. endinvoke (result); second, use its callback function mydelegate
Mydelegate
=
New
Mydelegate (Hello); mydelegate. begininvoke ("kntao", new asynccallback (completed), null); 8. Can I/O thread operations of the CLR thread pool be used as an example? The I/O thread is. net is a thread designed for accessing external resources, because access to external resources is often affected by external factors, in order to prevent the main thread from being affected for a long time in the blocking state ,. net has established asynchronous methods for multiple I/O operations, such as filestream, TCP/IP, webrequest, and WebService. In addition, each asynchronous method is used in a similar way, start with beginxxx and end with endxxx, 9. how can I avoid multi-threaded operations on the same data ?. NET provides multiple lock data methods, such as lock, monitor, and interlocked.

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.