. Net multithreading note (4): Thread Pool

Source: Internet
Author: User

. NetThread Pool

All the threads running in the thread pool are backend threads, and the isbackground attribute of the thread is set to true. The so-called backend threads mean that the running of these threads will not hinder the application.Program. On the contrary, the application can exit only after all foreground threads are finished.

Example

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreadtest {class program {static void main (string [] ARGs) {string taskobject = "running 10 seconds"; bool result = threadpool. queueuserworkitem (task, taskobject); If (! Result) console. writeline ("can't get thread"); else console. writeline ("press enter to exit"); console. read ();} static Void task (object state) {for (INT I = 0; I <10; I ++) {console. writeline (State + "thread ID is" + thread. currentthread. managedthreadid. tostring (); thread. sleep (1000 );}}}}

If you press any key during the program running, the program will be immediately aborted.

system. threading. the threadpool type encapsulates operations in the thread pool. Each process has a thread pool ,.. NET provides a thread pool management mechanism. You only need to insert the thread requirements into the thread pool, without having to ignore the subsequent work. All threads in the thread pool are backend threads and they will not hinder program exit.

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.