In the VS2005 environment, create a window form named multi-threaded application.
Complete code:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Threading;
Namespace multi-threaded applications
{
Public partial class Form1: Form
{
Public Form1 ()
{
Checkforillegalcrossthreadcils = false; // This method is not good, it is best to use the delegate
InitializeComponent ();
}
Private void btnCalculate_Click (object sender, EventArgs e)
{
TxtOutput. Text = "0 ";
LstMessage. Items. Clear ();
Int threadNumber = Convert.ToInt32(this.txt ThreadNumber. Text );
For (int I = 1; I <= threadNumber; I ++)
{
// ThreadStart threadStart = new ThreadStart (Add );
Thread thread = new Thread (new ThreadStart (this. Add ));
Thread. Name = I. ToString ();
Thread. Start ();
}
}
Private void Add ()
{
DateTime beginTime = DateTime. Now;
Long minValue = long. Parse (txtMinValue. Text );
Long maxValue = long. Parse (txtMaxValue. Text );
Int threadNumber = Convert. ToInt32 (txtThreadNumber. Text );
Int threadOrder = Convert. ToInt32 (Thread. CurrentThread. Name );
Long step = (maxValue-minValue + 1)/threadNumber;
Long beginValue = minValue + step * (threadOrder-1 );
Long endValue = beginValue + step;
Long result = 0;
For (long I = beginValue; I <endValue; I ++)
{
Result + = I;
}
Lock (this) // thread synchronization lock
{
Long sum = long. Parse (txtOutput. Text );
Sum + = result;
TxtOutput. Text = sum. ToString ();
}
DateTime endTime = DateTime. Now;
TimeSpan timeSpan = endTime-beginTime;
String message = "Thread" + Thread. currentThread. name + ":" + beginValue. toString () + "to" + endValue. toString () + ", time consumed:" + timeSpan. totalMilliseconds. toString () + "millisecond ";
This. lstMessage. Items. Add (message );
Thread. CurrentThread. Abort ();
}
Private void btnexit_click (Object sender, eventargs E)
{
Application. Exit ();
}
}
}
Summary:
Multithreading can improve the program running efficiency, but it is not absolute. The efficiency will not linearly increase with the increase in the number of threads, in addition, I will use delegation to solve some multithreading problems next time.
========================================================== ======================
Sleep: This method requires a long parameter, which indicates the number of milliseconds of sleep. You are not doing anything.
As for your doubts, you must first understand three concepts: the State in thread 3, ready state, blocking state, and running state.
The so-called multithreading does not mean that all threads run simultaneously. Instead, there is a time slice rotation mechanism to control who goes up and down.
For example, there is now an apple (similar to the CPU) and four children (four threads). When every child calls start (), they are in the ready state, then it's not that they both bite the apple together, but a bit (he is in the running state) and others (in the ready state). After a while, change one on the other (as for WHO to change, the probability is randomly selected based on the priority calculation), and then repeat one on the other.
For the sleep method, a parameter 8000, that is, mt1. Sleep (8000), that is, it takes 8 seconds for my child to run this statement. At this time, I am in a blocking state. In these 8 seconds, even if it is my turn, neither can I.
For priority, Thread has a method setPriority (int newPriority). If you pass a value of 10, it indicates the highest priority. If you pass a value of 1, it indicates the lowest priority. The default value is 5, the Thread class provides constants to represent them. That is to say, the opportunity for higher priority is large, and the opportunity for lower priority is small.
Conclusion: multithreading does not mean that multiple threads run at the same time, but takes turns to run according to the priority, but the cpu runs fast. Sometimes we think of it as running at the same time.