When the amount of data processing is large, how to respond to user operations while processing data? Windows95/98 and WindowsNT are multi-threaded multi-tasking operating systems. Their scheduling unit is a thread, that is, a thread is the basic unit for the system to allocate processor time resources. Therefore, we can use threads to process large batches of data while responding to user operations.
As an excellent development platform, DELPHI provides strong support for the development of multi-threaded applications. You can directly use the Win32API interface function CreateThread provided by the 32-bit Windows environment, or you can use the BeginThread function provided in DELPHI. In the following example, the TThread class provided by DELPHI is used.
one. The basic method is as follows:
1. Derive a new class from the Tthread class. (Create TSortThread class)
2. Define the Create method of the new class.
3. Define the Execute method of the new class, and insert the code executed when the thread is running in the Execute method.
4. Create instances using reference class methods.
two. Detailed code and description of the example:
First, create a new unit and save it as mysort.pas. In this unit, we created a TSortThread class, which inherits from the TThread class, so when we create an instance of this class in the program, we create Created a new thread.
Next, define a Sort method in this class to sort the array. At the same time, the TSortThread class transcends the constructor methods Create and Execute of the TThread class. In the execute method, the Sort method for sorting the array is called. The specific code is as follows:
unitmysort;
interface
usesClasses;//The TThread class is defined in Classes.
type
PSortArray=TSortArray;
TSortArray=array.[0..MaxIntdivSize??
Of(Integer)-1]ofInteger;
{TsortThread class defined here}
TSortThread=class(TThread)
PRivate
{The following private variables are defined in the TSortThread class}
fsortArray:PSortArray;
FSize:Integer;
FA,FB,FI,FJ:Integer;
Protected
{Class TSortThread transcends the Execute method of class Tthread}
procedure Execute;override;
{Class TsortThread adds a Sort method}
procedure Sort(varA:arrayofInteger);
public
{Class TSortThread transcends the constructor method of class Tthread}
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 thread starts, the Execute method will be called. }
procedure TSortThread.Execu??
te;
begin
Sort(Slice(FSortArray,FSize));
end;
{Bubble sorting is implemented below}
procedure TSortThread.Sort(varA:arrayofInteger);
var
I,J,T:Integer;
begin
forI:=High(A)downtoLow(A)do
forJ:=Low(A)toHigh(A)-1do
ifA[J]>A[J+1]then
begin
T:=A[J];
A[J]:=A[J+1];
A[J+1]:=T;
ifTerminated then Exit;
end;
end;
end
Finally, add usesmysort at the implementation of the user application, and add TQuickSortthread.Create(SortArray) where sorting is performed, where SortArray is an actual array. In this way, threads can be used to implement sorting. During the sorting process, users can perform other operations without waiting for the sorting to end. This method of using multiple threads to respond to user operations in real time is particularly important in applications involving large amounts of data processing.