Thread control-more multi-thread control methods

Source: Internet
Author: User
Tags sleep function

1. Sleep and Interrupt

The Sleep function blocks the current thread for a certain period of time. When the time arrives, the operating system continues scheduling and execution in the next time slice according to its scheduling algorithm.

The Interrupt function wakes up a thread in the WaitSleepJoin state. Essentially, the Interrupt function triggers ThreadInterryptedException to a thread in the WaitSleepJoin State. If the exception is not handled, it will continue to be thrown up,

 

Therefore, a best practice for writing multi-threaded programs is to use the try-catch Block in the main function of the thread to capture all possible exceptions.

2. Background thread and foreground thread

The IsBackground attribute control thread of a thread is a front-end thread or background thread. The front-end thread is only a little different from the background thread, and the background thread will not stop terminating. If all foreground threads of a process terminate, CLR ends the process, and other background running threads are also forcibly terminated without waiting for execution to complete.

 

3. Suspend and Resume

Suspend and Resume are two obsolete functions. The Suspend function converts the thread to the suincluded state, and the Resume function converts the thread from the suincluded state to the running state.


From Enjoy. NET

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.