Single thread and multithreading in C #

Source: Internet
Author: User

1. The difference between a single thread and multiple threads and the advantages of multiple threads

When a single-threaded program is executed, the program path (processing items) runs in a continuous order and must be processed first, and then painted later.
Multithreading. For example, a program can execute more than two identical operations at the same time, such as multiple-threaded software that searches for proxies or sends emails in a group, because an operation takes a long time to return network information, but it is idle for the CPU, it takes a long time to search for thousands of IP addresses if it is executed sequentially. If multithreading is used, you can add other searches while waiting, and then wait, which can improve efficiency.
2. The following is an example of how to implement the content between a single thread and multiple threads. You can tell the difference between a single thread and multiple threads in this example. This also depends on the speed at which each computer runs, may be related to the running speed of their computers, so you may see different thread running results

 

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
// Single-thread content
/* Stopwatch stwatch = new stopwatch ();
Stwatch. Start ();
Printnumb ();
Printstr ();
Stwatch. Stop ();
Console. writeline (stwatch. elapsed. totalmilliseconds );
Console. readkey ();*/

// Thread this is perhaps the most complicated method, but it provides various flexible control over the thread. First, you must use its constructor to create a thread instance. Its parameters are relatively simple and there is only one threadstart delegate.
// Multi-threaded content
Threadstart printnumb = new threadstart (printnumb );
Thread readnum = new thread (printnumb );
Threadstart printstr = new threadstart (printstr );
Thread readstr = new thread (printstr );

 

// Next, we will learn about thread control based on the threadstate attribute of the thread. Threadstate is an enumeration type that reflects the state of a thread. When a thread instance is just created, its

// Threadstate is unstarted; when this thread is started by calling start (), its threadstate is running;

Stopwatch star = new stopwatch ();
Star. Start ();
Readnum. Start ();
Readstr. Start ();

While (true)
{
If (readstr. threadstate = system. Threading. threadstate. Stopped & readnum. threadstate = system. Threading. threadstate. Stopped)
{
Star. Stop ();
Console. writeline (Star. elapsed. totalmilliseconds );
Break;
}
}
Console. readkey ();
}

Public static void printnumb ()
{
For (INT I = 0; I <1000; I ++)
{
Console. writeline (I );
}
}
Public static void printstr ()
{
For (INT I = 0; I <1000; I ++)
{
Console. writeline ("output" + I. tostring ());
}
}
}
}

 

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.