Reading directory
I. Preface
Ii. multi-threaded array sorting
I. Preface
The array size is the number of array elements to be sorted, and the number of threads is the number of threads you plan to use to sort.
Ii. multi-threaded array sorting
Private int [] array;
Private DateTime StartTime;
Private DateTime EndTime;
Private static volatile bool swaped = true;
Private static long threadCounter = 0;
Private static volatile string strng = "";
Protected void Page_Load (object sender, EventArgs e)
{
If (Application ["ThreadAndTime"]! = Null)
{
TxtResult. Text = Application ["ThreadAndTime"]. ToString ();
TxtOut. Text = Application ["OutTxt"]. ToString ();
}
}
/// <Summary>
/// The sort button is clicked.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnSort_Click (object sender, EventArgs e)
{
BtnSort. Enabled = false;
Int ArrayCount = Convert. ToInt16 (ddlArrayCount. SelectedItem. Text. Trim ());
Int ThreadCount = Convert. ToInt16 (ddlThreadCount. SelectedItem. Text. Trim ());
Array = new int [ArrayCount];
Array. Initialize ();
ThreadCounter = 0;
LblMessage. Text = "sorting ......";
// First, sort the elements in the array in reverse order.
For (int I = 0; I <array. Length; I ++)
{
Array [I] = array. Length-I;
}
StartTime = DateTime. Now;
For (int I = 0; I <ThreadCount; I ++)
{
Thread thread = new Thread (new ThreadStart (SortArray ));
Thread. Name = I. ToString ();
Thread. Start ();
}
Page. RegisterStartupScript ("", "<script> window. setTimeout ('location. href = location. href ', 5000); </script> ");
}
/// <Summary>
/// Method for sorting
/// </Summary>
Private void SortArray ()
{
Try
{
While (true)
{
Swaped = false;
For (int I = 0; I <array. Length-1; I ++)
{
Lock (typeof (Thread ))
{
If (array [I]> array [I + 1])
{
Int temp = array [I];
Array [I] = array [I + 1];
Array [I + 1] = temp;
Swaped = true;
}
}
}
Thread. Sleep (1 );
If (! Swaped)
{
Break;
}
}
Thread. CurrentThread. Abort ();
}
Catch (Exception ex)
{
If (Interlocked. Increment (ref threadCounter) = Convert. ToInt64 (ddlThreadCount. SelectedItem. Text. ToString (). Trim ()))
{
DisplayInfo ();
}
}
}
/// <Summary>
/// Display the sorted Information
/// </Summary>
Public void DisplayInfo ()
{
LblMessage. Text = "sorting ends ......";
Strng = "";
For (int I = 0; I <array. Length; I ++)
{
Strng + = array [I]. ToString () + "";
}
EndTime = DateTime. Now;
BtnSort. Enabled = true;
TimeSpan timespan = EndTime-StartTime; // time span
Application ["ThreadAndTime"] + = "Threads:" + ddlThreadCount. selectedItem. text. toString (). trim () + "milliseconds used:" + Convert. toString (timespan. totalMilliseconds) + "\ r \ n ";
Application ["OutTxt"] = strng + "\ r \ n ";
}
/// <Summary>
/// Clear the control information after sorting the Array
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnEmptyOne_Click (object sender, EventArgs e)
{
Application ["OutTxt"] = "";
TxtOut. Text = "";
}
/// <Summary>
/// Clear the sorted Information
/// </Summary>
/// <Param name = "sender"> </param>
Protected void btnEmptyTwo_Click (object sender, EventArgs e)
{
Application ["ThreadAndTime"] = "";
TxtResult. Text = "";
}
We select the array size as 500 elements and use one thread for sorting. We found that it took 1109.375 milliseconds. We know that 1000 milliseconds equals 1 second, that is to say, it takes more than one second to sort the array with one thread and the array with a size of 500 elements.
We still select the array size as 500 elements and use five threads for sorting. We found that it took 328.125 milliseconds. We know that 1000 milliseconds equals 1 second, that is, 0.3 seconds, that is to say, it takes more than 500 seconds for an array of 0.3 elements to be sorted by five threads. One problem is that I call the Thread. currentThread. abort () then an exception is reported, so I put the sorted output information function DisplayInfo into catch. If someone can tell me why an exception is reported, I am not very grateful.