多线程问题,请教。(50分)

  • 主题发起人 主题发起人 qddmh
  • 开始时间 开始时间
Q

qddmh

Unregistered / Unconfirmed
GUEST, unregistred user!
1、
在Delphi书多线程部分中讲到"临界区", "互斥元", "THread对象" 3部分
1: 我想问是否"临界区"与"互斥元"就是为了保持多线程间同步?
其中"临界区"是为保持同一进程间不同线程同步, 尔"互斥元"
既能保持同一进程间不同线程同步也能保持不同进程间线程同步?
如果是这样是否"互斥元"可以取代"临界区"?
2: Thread 是否就是用于保证VCL的线程安全性?它的同步是否就是用
Synchronize函数保证的?

3: 像FlashGet, NetAnts等断点续传多线程软件用的是那中方式实现的?
它们下载方式是否将下载目标拆分为多个快, 每个线程下载一个快, 当
全部下载完后在合并?
如果是这样那开多个线程比如100个肯定下载速度非常快,但为什么不是?

4: 比如当用FlashGet开5个线程下载一目标是否比用IE直接下载快,如果是
为什么?

5: 下面程序用于循环显示从1---10000的数, 当我用线程显示时比不用线程慢
很多.是否说明在单机中进行类似操作时, 不用多线程比用多线程快,
利用多线程只是为了实现多任务?
欢迎大家给与解答讨论
//////////////////////////////////////////////////////////////////////////
// This Example has a Form and a Unit. //
// There are one Panel and three Buttons on the form. //
// The Panel Display Changing Numbers. //
// //
//////////////////////////////////////////////////////////////////////////
unit MainThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, BadThread, GoodThread;
type
TForm1 = class(TForm)
GoodThread: TButton;
BadThread: TButton;
Panel1: TPanel;
NoThread: TButton;
procedure GoodThreadClick(Sender: TObject);
procedure BadThreadClick(Sender: TObject);
procedure NoThreadClick(Sender: TObject);
private
{ Private declarations }
procedure BadThreadDone(Sender: TObject);
procedure GoodThreadDone(Sender: TObject);
procedure ButtonOff(Setting: Boolean);
public
{ Public declarations }
end;

var
Form1: TForm1;
myGoodThread: TGoodThread;
myBadThread: TBadThread;
begin
Time: LongWord;
EndTime: LongWord;
implementation
{$R *.dfm}
procedure TForm1.BadThreadDone(Sender: TObject);
begin
myBadThread.Free;
EndTime := GetTickCount;
Caption := IntToStr(EndTime - begin
Time);
ButtonOff(False);
end;

procedure TForm1.GoodThreadClick(Sender: TObject);
begin
ButtonOff(True);
begin
Time := GetTickCount;
myGoodThread := TGoodThread.Create(False);
myGoodThread.Onterminate := GoodThreadDone;
end;

procedure TForm1.BadThreadClick(Sender: TObject);
begin
ButtonOff(True);
begin
Time := GetTickCount;
myBadThread := TBadThread.Create(False);
myBadThread.Onterminate := BadThreadDone;
end;

procedure TForm1.ButtonOff(Setting: Boolean);
var
i: integer;
begin
for i:= 0 to Self.ComponentCount - 1do
begin
if Components is TButton then
TButton(Components).Enabled := not Setting;
end;
end;

procedure TForm1.GoodThreadDone(Sender: TObject);
begin
myGoodThread.Free;
EndTime := GetTickCount;
Caption := IntToStr(EndTime - begin
Time);
ButtonOff(False);
end;

procedure TForm1.NoThreadClick(Sender: TObject);
var
i: integer;
begin
begin
Time := GetTickCount;
for i := 0 to 10000do
begin
Panel1.Caption := inttostr(i);
Panel1.Update ;
end;
EndTime := GetTickCount;
Caption := IntToStr(Endtime - begin
Time);
end;

end.

/////////////////////////////////////////////////////////////////////////
// This Unit has Two Threads, one is TBadThread,
// the other is TGoodThread.
// The TBadThread is a bad Thread and The TGoodThread is a good
// Thread.
//
/////////////////////////////////////////////////////////////////////////
unit BadThread;
interface
uses
Windows, Classes, SysUtils;
type
TBadThread = class(TThread)
private
procedure ExecUpdate;
protected
procedure Execute;
override;
end;
TGoodThread = class(TThread)
private
FCount: Integer;
procedure ExecUpdate;
protected
procedure Execute;
override;
end;
implementation
uses MainThread;
{ TBadThread }
procedure TBadThread.ExecUpdate;
var
i: integer;
begin
for i := 0 to 10000do
begin
Form1.Panel1.Caption := IntToStr(i);
Form1.Panel1.Update;
end;
end;

procedure TBadThread.Execute;
begin
Windows.InvalidateRect(Form1.Handle, nil, True);
Synchronize(ExecUpdate);
end;

{ TGoodThread }
procedure TGoodThread.Execute;
var
i: Integer;
begin
Windows.InvalidateRect(Form1.Handle, nil, True);
for i := 0 to 10000do
begin
FCount := i;
Synchronize(ExecUpdate);
end;
end;
procedure TGoodThread.ExecUpdate;
begin
Form1.Panel1.Caption := inttostr(FCount);
end;

end.


其它:
1、sniffer这个工具主要用来干什么的?
2、现在, DataSnap(Midas)用的广不广?
能否给一个这样的例子?

 
1.1 当然可以
1.2 Thread 就是线程。 Synchronize函数用来保持同步
1.3 和网速有关,而且线程多了,每个线程分到的时间就少了。同时加重了服务器的负担
1.4 应该是。因为你从服务器多分了点时间。
1.5 应该不会慢很多。这种情况体现不出多线程的作用
 
TcriticalSection 也可以同步资源
 

Similar threads

后退
顶部