. NET Windows Multithreading Thread Programming _ Practical Tips

Source: Internet
Author: User
Tags net thread
Process: Factory to carry bricks, 10 workers to carry out 1000 bricks
Threads: Each worker, after moving 100 bricks, is a task
Using threads, asynchronous moving:
Hand to move, 10 workers to move at the same time, high efficiency, asynchronous execution, if not using the thread, and so the first person to move after the second talent move, then slow

when you can't use threads
If you want to use the car to move, only a small car, then, in such cases, should not use the thread, because only a car, each thread is in contention and waiting for this car, so can not use the thread. If you want to use threads at this time, it will cause waste of resources,
Because each thread has its own resources, such as the contractor (CPU) at the same time to manage the 10 workers, and only one worker is actually doing things.
Thread Destruction: In the destruction of the time to consider some of the problems, such as moving bricks, you can not when a worker moved to the halfway, you will destroy him, where is the brick?

1..net Thread Usage
. NET uses threads, thread classes, define threads, start, destroy, all included in this class to define a thread, you must specify the function of this thread, which is what this thread does, moving bricks or fetching water
EG:
Copy Code code as follows:

Thread t = new Thread (new ThreadStart (ST). Workermethod));
T.start ();
public void Workermethod ()
{
Console.WriteLine ("AA");
}

2. If you use a thread with parameters
The thread itself can not take parameters, can only define a global variable in the class, the thread first to assign the variable value, and then in the function of the thread, to use this variable, to achieve the purpose of passing parameters
EG:
Copy Code code as follows:

public class Simplethread
{
private string procparameter = "";
Public Simplethread (String Strpara)
{
Procparameter = Strpara;
}
public void Workermethod ()
{
Console.WriteLine ("Parameter input is:" + procparameter);
}
}
Class MainClass
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Simplethread st = new Simplethread ("This is the parameter string!");
Thread t = new Thread (new ThreadStart (ST). Workermethod));
T.start ();
T.join (Timeout.infinite);
}
}

3. Threads and Delegates
Purpose of Delegation: similar to C + + refers to the clock, that is, the function as a parameter to pass
4. Application domain
10 workers each move their own, separated, not mutually affected, a person complained not to move, does not affect others, the computer is the same situation, the various applications can not interfere between the program can not access, and a program died, the entire computer department can panic.
5. Thread pool
As in memory, if workers move bricks, suddenly 2 workers for whatever reason can not move, then from the line Cheng Chili call other threads to do, good performance, create a thread need to waste time and resources
6. Asynchronous Call
Call WebService for example
Sync: When you call a webservice, the call is over to execute the following code, and if it's blocked at the time of the call, it's going to be a long wait.
Asynchronous call: Call WebService, code continues to execute, webservice after execution, and then back to the receiver object.
7. Multithreading applications, using thread sorting
Sort 1000 number, if one thread platoon, it takes 1000 milliseconds
If 10 threads are queued, it only takes about 100 milliseconds
Of course, the sort of time a[i] a[i+1], and only one thread can operate on him
Therefore, in the for inside, I and i+1 exchange, you need to lock in advance.
Copy Code code as follows:

for (int t = 0; t < x; t++)//x number of threads
{
Thread thread = new Thread (New ThreadStart (sort))//sort the method of sorting
thread. Name = convert.tostring (t);
Thread. Start ();
}
public void Sort ()
{
Try
{
while (true)
{
swaped = false;
for (int j = 0; J < Valuearray.length-1; J + +)
{
Lock (Thread)/lock
{
If typeof Valuearra Y[J] > valuearray[j + 1])
{
int T = valuearray[j];
Valuearray[j] = valuearray[j + 1];
Valuearray[j + 1] = T;
swaped = true;
}
}
}
Thread.Sleep (1);
if (!swaped) {break;}
}
Thread.CurrentThread.Abort ();
}
catch (Exception ex)
{
if (interlocked.increment (ref threadcounter) = = Convert.toint64 (ddlthreadnu M.selecteditem.tostring (). ToString (). Trim ())
Display ();
}
}

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.