unitmysort; interface usesClasses;//TThread類在Classes中被定義。 type PSortArray=TSortArray; TSortArray=array.[0..MaxIntdivSize?? Of(Integer)-1]ofInteger; {此處定義了TsortThread類} TSortThread=class(TThread) PRivate {在TSortThread類中定義了如下幾個私有變元} fsortArray:PSortArray; FSize:Integer; FA,FB,FI,FJ:Integer; Protected {類TSortThread超越了類Tthread的Execute方法} procedure Execute;override; {類TsortThread添加了一個Sort方法} procedure Sort(varA:arrayofInteger); public {類TSortThread超越了類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; {當線程開始時,Execute方法將被調用。} procedure TSortThread.Execu?? te; begin Sort(Slice(FSortArray,FSize)); end; {下面實現了冒泡法排序} 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 if A[J]>A[J+1] then begin T:=A[J]; A[J]:=A[J+1]; A[J+1]:=T; if Terminated then Exit; end; end; end