C # multi-thread Development 2: thread suspension, recovery and Suspension

Source: Internet
Author: User

This article introduces the creation and startup of threads in the C # multi-Thread development 1: Using Thread classes to create and start threads. Based on the previous article, this article introduces operations such as thread suspension, recovery, and suspension. First, let's recall the previous knowledge points. We have learned: 1. Create a Thread [csharp] ThreadStart entry = new ThreadStart (CalcSum); Thread workThread = new Thread (entry ); or [csharp] Thread workThread = new Thread (new ThreadStart (CalcSum); Thread Entry Method: [csharp] static void CalcSum () {// do what you want} 2. Start the thread [csharp] workThread. start (); next, we will study other operations on the thread. The instance used in this article is obtained after a small change on the basis of the previous instance. At the beginning, the complete instance code and running results (for the whole instance operation) are provided ). [Csharp] using System; using System. threading; namespace ThreadExample {class Program {static void Main (string [] args) {ThreadStart entry = new ThreadStart (CalcSum); Thread workThread = new Thread (entry); workThread. start (); CalcGap () ;}static void CalcSum () {long sum = 0; for (long I = 0; I <1000000000; I ++) {sum + = I; if (I % 100000000 = 0 & I! = 0) {Console. writeLine ("workThread --> I = {0}: sum = {1}", I, sum) ;}} static void CalcGap () {long gap = 0; for (long I = 1000000000; I> = 0; I --) {gap = I-1; if (I % 100000000 = 0 & I! = 0) {Console. writeLine ("MainThread --> I = {0}: gap = {1}", I, gap) ;}}} running result: two threads exist in the instance, the Main thread (Main function) and the working thread (workThread), the Main thread completes the difference operation, and the working thread completes the sum operation. The two threads run simultaneously. 3. Use the Suspend () method to Suspend a thread. After a thread is suspended, the operation is stopped or sleep. The suspended thread does not occupy any processing time. Modify the previous instance Code in the following sections: [csharp] ThreadStart entry = new ThreadStart (CalcSum); Thread workThread = new Thread (entry); workThread. start (); workThread. suspend (); // Suspend the thread (CHANGE Part) CalcGap (); execute the program again and get the following results: The result shows that the main thread runs normally at this time, however, the workThread of the working thread is not executed because it is suspended after being started, so it will not be executed. To continue executing the job thread, You need to restore the thread. 4. Use the Resume () method to restore the thread. Modify the code again. [Csharp] Thread workThread = new Thread (new ThreadStart (CalcSum); workThread. start (); workThread. suspend (); // suspends the thread (CHANGE Part) CalcGap (); workThread. resume (); // The code above the restoration thread (CHANGE Part). After the main thread completes execution, it restores the suspended working thread. The execution result is as follows. We can predict this result.

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.