. NET Windows multi-thread Programming

Source: Internet
Author: User

Process: Factory bricks, 10 workers finished 1000 bricks
Thread: Every worker who has moved 100 bricks is a task.
Thread-based Asynchronous migration:
Manual migration: 10 workers move at the same time, with high efficiency and asynchronous execution. If no thread is used, wait until the first person completes the migration and the second person moves.

When cannot I use a thread?:
If you want to use a car to move, there is only one car, then in this case, you should not use a thread, because there is only one car, each thread is competing for and waiting for this car, therefore, threads cannot be used. If a thread is used at this time, the resource is wasted,
Because each thread has its own resources, such as the contractor's head (CPU) to manage these 10 workers at the same time, only one worker is actually doing things.
Thread destruction: Some problems need to be considered during the destruction process. For example, when you move a brick, you cannot destroy it when a worker moves halfway. Where should you put the brick?

Use threads in 1..net
. Net using threads, using thread classes, defining threads, starting and destroying, all including defining a thread in this class, the function of this thread must be specified, that is, what the thread is doing, whether to move bricks or pick water
EG:
Copy codeThe Code is as follows:
Thread t = new Thread (new ThreadStart (st. WorkerMethod ));
T. Start ();
Public void WorkerMethod ()
{
Console. WriteLine ("AA ");
}

2. If a thread with parameters is used
The thread itself cannot contain parameters. It can only define a global variable in the class. The variable is assigned a value before the thread, and then used in the thread function, to pass Parameters
EG:
Copy codeThe Code is as follows:
Public class SimpleThread
{
Private string procParameter = "";
Public SimpleThread (string strPara)
{
ProcParameter = strPara;
}
Public void WorkerMethod ()
{
Console. WriteLine ("parameter input:" + procParameter );
}
}
Class MainClass
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] args)
{
SimpleThread st = new SimpleThread ("this is a parameter string! ");
Thread t = new Thread (new ThreadStart (st. WorkerMethod ));
T. Start ();
T. Join (Timeout. Infinite );
}
}

3. threads and Delegation
The purpose of delegation is to pass functions as parameters, similar to the C ++ clock.
4. Application domain
Each of the 10 workers moved to their respective locations, isolated from each other, and not affected each other. One person complained that the migration would not affect others, and the same would happen on the computer. Applications could not interfere with each other, no access is allowed between programs, and a program is dead, and the entire computer brain can crash.
5. Thread Pool
Just like the memory, if two workers suddenly fail to move bricks, then the other threads are called from the thread pool for good performance, creating a thread wastes time and resources.
6. asynchronous call
WebService is called as an example.
Synchronization: When a WebService is called, the following code is executed after the WebService is called. If it is blocked during the call, it takes a long time to wait.
Asynchronous call: Call WebService. The Code continues to be executed. After WebService is executed, it is passed back to the receiver object.
7. multi-threaded applications that utilize thread sorting
Sort the number by 1000. If a thread is in a row, it takes 1000 milliseconds.
If there are 10 threads, it takes about 100 milliseconds.
Of course, when sorting, a [I] a [I + 1] can only be operated by one thread.
Therefore, in For, I and I + 1 must be locked in advance.
Copy codeThe Code is as follows:
For (int t = 0; t <x; t ++) // X indicates the number of threads.
{
Thread thread = new Thread (new ThreadStart (Sort); // Method for sorting Sort
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 (typeof (Thread) // lock
{
If (valueArray [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 (ddlThreadNum. SelectedItem. ToString (). ToString (). Trim ()))
Display ();
}
}

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.