How is the thread sort of

Author:Anonymous    Updated:2008-2-26 22:03:18
Dealing with the tremendous volume of data in the circumstances, how the processing of the data in response to user operation at the same time? Windows95/98 WindowsNT as multi-threading and multi-tasking operating systems and thread scheduling module is that the distribution system is threaded processor resources the basic unit of time, so we can use threads to achieve in dealing with large quantities of data at the same time response to user operation.
DELPHI as an excellent development platform for the development of multi-threaded applications to provide strong support, can be the direct use of 32-bit Windows environment, the interface function CreateThread Win32API, can be used in the DELPHI BeginThread function. In the following example, it is precisely the use of the DELPHI TThread category.
1. Basic approach is as follows:
1. Tthread derived from the category of a new type. (Creates TSortThread)
2. Create a new definition of the method.
3. Execute the new definition of the category, and Execute method to insert threads implementation of the run-time code.
4. Cited examples of ways to create.
2. And the detailed code examples:
First, a new unit, for the preservation of mysort.pas in this module, we created a TSortThread category, and it TThread inherited from the category, so when we created this program in such a case, it is creating a a new thread.
Then, in the definition of such a Sort method used to sort the array, while TSortThread TThread category beyond the constructor method Create and Execute, execute method, called a sort of the array Sort method . Specific code as follows:
Unitmysort;
Interface
UsesClasses; / / TThread category, which is defined in the Classes.
Type
PSortArray = TSortArray;
TSortArray = array. [0 .. MaxIntdivSize 
Of (Integer) -1] ofInteger;
(TsortThread here defined category)
TSortThread = class (TThread)
Private
(TSortThread category in the definition of the following private variables)
FSortArray: PSortArray;
FSize: Integer;
FA, FB, FI, FJ: Integer;
Protected
(TSortThread beyond the category of Tthread the Execute method)
Procedure Execute; override;
(Category TsortThread added a Sort method)
Procedure Sort (varA: arrayofInteger);
Public
(TSortThread beyond the category of Tthread the constructor)
ConstructorCreate (varSortArray: arrayofInteger);
End;
Implementation
ConstructorTSortThread.Create (varSortArray: arrayofInteger);
Begin
FSortArray: = @ SortArray;
FSize: = High (SortArray)-Low (SortArray) +1;
FreeOn Terminate: = True;
InheritedCreate (False);
End;
(When the threads at the beginning Execute method will be called. )
Procedure TSortThread.Execu 
Te;
Begin
Sort (Slice (FSortArray, FSize));
  end;
(Below the edge of achieving the sort)
Procedure TSortThread.Sort (varA: arrayofInteger);
Var
I, J, T: Integer;
Begin
For I: = High (A) downto Low (A) do
For J: = Low (A) to High (A) -1 do
[J] if A> A then [J +1]
Begin
T: = A [J];
A [J]: = A [J +1];
A [J +1]: = T;
If Terminated then Exit;
End;
End;
End
Finally, in the implementation of user applications by adding usesmysort, in the implementation of sort of joining TQuickSortthread.Create (SortArray), which is a real SortArray array. This thread can be used to achieve order in the ranking process, users do not have to wait until the end of sort can perform other operations. This multi-threaded real-time response to user operation involving large amounts of data processing in the application appears to be particularly important.
 
Previous:Delphi prepared by an analytic function of the FTP address
Next:Delphi programming interesting list
User Reviews
Related Articles
Recommended article
AD