多线程提问,create里的参数好像没用的阿?
以下是我的测试程序,是希望输出以下结果
1
2
1
2
3
但实际运行过程中,得输出结果却是
1
2
3
1
2
3 也就是说,后一个创建的线程运行了两次,请问各位,为什么会这样呢??有什么办法能让一个线程结束后才让后一个线程运行呢??? 多些指教。。。。。
type
Tmythread = class(Tthread)
protected
procedure execute; override;
public
constructor create(i:integer);
procedure shownumber;
end;
var
Form1: TForm1;
count:integer;
implementation
{$R *.dfm}
procedure Tmythread.execute;
begin
synchronize(shownumber);
end;
procedure Tmythread.shownumber;
var j:integer;
begin
for j:=1 to count do
begin
form1.Memo1.Lines.Add(inttostr(j)) ;
sleep(2);
end;
end;
constructor Tmythread.create(i:integer);
begin
freeonterminate:=true;
inherited create(true);
count:=i;
resume;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Tmythread.create(2);
Tmythread.create(3);
end;