第六节: TList 与泛型
 
TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼。
我们先来改进一下带泛型的 TList 基类,以便以后使用。
本例源码下载(delphi XE8版本): FooList.Zip
 
unit uFooList;
interface
uses
  Generics.Collections;
type
 
  TFooList <T>= class(TList<T>)
  private
    procedure FreeAllItems;
  protected
    procedure FreeItem(Item: T);virtual;
    // 子类中需要重载此过程。以确定到底如何释放 Item
    // 如果是 Item 是指针,就用 Dispose(Item);
    // 如果是 Item 是TObject ,就用 Item.free;
  public
    destructor Destroy;override;
    procedure ClearAllItems;
    procedure Lock;  // 给本类设计一把锁。
    procedure Unlock;
  end;
  // 定义加入到 List 的 Item 都由 List 来释放。
  // 定义释放规则很重要!只有规则清楚了,才不会乱套。
  // 通过这样简单的改造, TList 立马好用 N 倍。
 
implementation
{ TFooList<T> }
 
procedure TFooList<T>.ClearAllItems;
begin
  FreeAllItems;
  Clear;
end;
 
destructor TFooList<T>.Destroy;
begin
  FreeAllItems;
  inherited;
end;
 
procedure TFooList<T>.FreeAllItems;
var
  Item: T;
begin
  for Item in self do
    FreeItem(Item);
end;
 
procedure TFooList<T>.FreeItem(Item: T);
begin
end;
 
procedure TFooList<T>.Lock;
begin
  System.TMonitor.Enter(self);
end;
 
procedure TFooList<T>.Unlock;
begin
  System.TMonitor.Exit(self);
end;
 
end.
 
将第五节的例子用 TFooList 改写:
 
unit uFrmMain; 
interface 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, uCountThread, uFooList;
 
type
 
  TCountThreadList = Class(TFooList<TCountThread>) // 定义一个线程 List
  protected
    procedure FreeItem(Item: TCountThread); override; // 指定 Item 的释放方式。
  end;
 
  TNumList = Class(TFooList<Integer>); // 定义一个 Integer List
 
  TFrmMain = class(TForm)
    memMsg: TMemo;
    edtNum: TEdit;
    btnWork: TButton;
    lblInfo: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnWorkClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
 
    FNumList: TNumList;
    FCountThreadList: TCountThreadList;
 
    FBuff: TStringList;
    FBuffIndex: Integer;
    FBuffMaxIndex: Integer;
    FWorkedCount: Integer;
 
    procedure DispMsg(AMsg: string);
    procedure OnThreadMsg(AMsg: string);
 
    function OnGetNum(Sender: TCountThread): Boolean;
    procedure OnCounted(Sender: TCountThread);
 
    procedure LockCount;
    procedure UnlockCount;
 
  public
    { Public declarations }
  end;
 
var
  FrmMain: TFrmMain;
 
implementation
 
{$R *.dfm}
{ TFrmMain }
 
{ TCountThreadList }
procedure TCountThreadList.FreeItem(Item: TCountThread);
begin
  inherited;
  Item.Free;
end;
 
procedure TFrmMain.btnWorkClick(Sender: TObject);
var
  s: string;
  thd: TCountThread;
begin
 
  btnWork.Enabled := false;
  FWorkedCount := 0;
  FBuffIndex := 0;
  FBuffMaxIndex := FNumList.Count - 1;
 
  s := '共' + IntToStr(FBuffMaxIndex + 1) + '个任务,已完成:' + IntToStr(FWorkedCount);
  lblInfo.Caption := s;
 
  for thd in FCountThreadList do
  begin
    thd.StartThread;
  end;
 
end;
 
procedure TFrmMain.DispMsg(AMsg: string);
begin
  memMsg.Lines.Add(AMsg);
end;
 
procedure TFrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // 防止计算期间退出
  LockCount; // 请思考,这里为什么要用 LockCount;
  CanClose := btnWork.Enabled;
  if not btnWork.Enabled then
    DispMsg('正在计算,不准退出!');
  UnlockCount;
end;
 
procedure TFrmMain.FormCreate(Sender: TObject);
var
  thd: TCountThread;
  i: Integer;
begin
 
  FCountThreadList := TCountThreadList.Create;
  // 可以看出用了 List 之后,线程数量指定更加灵活。
  // 多个线程在一个 List 中,这个 List 可以理解为线程池。
  for i := 1 to 3 do
  begin
    thd := TCountThread.Create(false);
    FCountThreadList.Add(thd);
    thd.OnStatusMsg := self.OnThreadMsg;
    thd.OnGetNum := self.OnGetNum;
    thd.OnCounted := self.OnCounted;
    thd.ThreadName := '线程' + IntToStr(i);
  end;
 
  FNumList := TNumList.Create;
  // 构造一组数据用来测试
  FNumList.Add(100);
  FNumList.Add(136);
  FNumList.Add(306);
  FNumList.Add(156);
  FNumList.Add(152);
  FNumList.Add(106);
  FNumList.Add(306);
  FNumList.Add(156);
  FNumList.Add(655);
  FNumList.Add(53);
  FNumList.Add(99);
  FNumList.Add(157);
 
end;
 
procedure TFrmMain.FormDestroy(Sender: TObject);
begin
  FNumList.Free;
  FCountThreadList.Free;
end;
 
procedure TFrmMain.LockCount;
begin
  System.TMonitor.Enter(btnWork);
end;
 
procedure TFrmMain.UnlockCount;
begin
  System.TMonitor.Exit(btnWork);
end;
 
procedure TFrmMain.OnCounted(Sender: TCountThread);
var
  s: string;
begin
 
  LockCount;
  // 锁不同的对象,宜用不同的锁。
  // 每把锁的功能要单一,锁的粒度要最小化。才能提高效率。
 
  s := Sender.ThreadName + ':' + IntToStr(Sender.Num) + '累加和为:';
  s := s + IntToStr(Sender.Total);
  OnThreadMsg(s);
 
  inc(FWorkedCount);
 
  s := '共' + IntToStr(FBuffMaxIndex + 1) + '个任务,已完成:' + IntToStr(FWorkedCount);
 
  TThread.Synchronize(nil,
    procedure
    begin
      lblInfo.Caption := s;
    end);
 
  if FWorkedCount >= FBuffMaxIndex + 1 then
  begin
    TThread.Synchronize(nil,
      procedure
      begin
        DispMsg('已计算完成');
        btnWork.Enabled := true// 恢复按钮状态。
      end);
  end;
 
  UnlockCount;
 
end;
 
function TFrmMain.OnGetNum(Sender: TCountThread): Boolean;
begin
  // 将多个线程访问 FNumList 排队。
  FNumList.Lock;
  try
    if FBuffIndex > FBuffMaxIndex then
    begin
      result := false;
    end
    else
    begin
      Sender.Num := FNumList[FBuffIndex];
      result := true;
      inc(FBuffIndex);
    end;
  finally
    FNumList.Unlock;
  end;
end;
 
procedure TFrmMain.OnThreadMsg(AMsg: string);
begin
  TThread.Synchronize(nil,
    procedure
    begin
      DispMsg(AMsg);
    end);
end;
 
end.
 
通过这五节学习,相信大家已能掌握 delphi 线程的用法了。
下一节课程内容,我们将利用 TFooList 设计更高级实用的线程工具。也是本线程教程的完结篇。


delphi 线程教学第六节:TList与泛型的更多相关文章

  1. delphi 线程教学第五节:多个线程同时执行相同的任务

    第五节:多个线程同时执行相同的任务   1.锁   设,有一个房间 X ,X为全局变量,它有两个函数  X.Lock 与 X.UnLock; 有如下代码:   X.Lock;      访问资源 P; ...

  2. delphi 线程教学第四节:多线程类的改进

    第四节:多线程类的改进   1.需要改进的地方   a) 让线程类结束时不自动释放,以便符合 delphi 的用法.即 FreeOnTerminate:=false; b) 改造 Create 的参数 ...

  3. delphi 线程教学第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行

    第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行     以 Ado 为例,常见的方法是拖一个 AdoConnection 在窗口上(或 DataModule 中), 再配合 AdoQ ...

  4. delphi 线程教学第二节:在线程时空中操作界面(UI)

    第二节:在线程时空中操作界面(UI)   1.为什么要用 TThread ?   TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...

  5. delphi 线程教学第一节:初识多线程

    第一节:初识多线程   1.为什么要学习多线程编程?   多线程(多个线程同时运行)编程,亦可称之为异步编程. 有了多线程,主界面才不会因为耗时代码而造成“假死“状态. 有了多线程,才能使多个任务同时 ...

  6. delphi 线程教学第一节:初识多线程(讲的比较浅显),还有三个例子

    http://www.cnblogs.com/lackey/p/6297115.html 几个例子: http://www.cnblogs.com/lackey/p/5371544.html

  7. delphi 线程教学第三节:设计一个有生命力的工作线程

    第三节:设计一个有生命力的工作线程   创建一个线程,用完即扔.相信很多初学者都曾这样使用过. 频繁创建释放线程,会浪费大量资源的,不科学.   1.如何让多线程能多次被复用?   关键是不让代码退出 ...

  8. ASP.NET MVC深入浅出系列(持续更新) ORM系列之Entity FrameWork详解(持续更新) 第十六节:语法总结(3)(C#6.0和C#7.0新语法) 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字 各种通讯连接方式 设计模式篇 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借

    ASP.NET MVC深入浅出系列(持续更新)   一. ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态模 ...

  9. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

随机推荐

  1. java 中文乱码问题,请注意response.getWriter的顺序

    反例: 正例:

  2. 解析 Javascript - this

    在函数中 this 到底取何值,是在函数真正被调用执行的时候确定下来的,函数定义的时候确定不了.  因为 this 的取值是执行上下文环境的一部分,每次调用函数,都会产生一个新的执行上下文环境.当你在 ...

  3. linux系统下Apache日志分割(按天生成文件)

    Apache日志按天显示,修改Apache http.conf文件,注释默认的日志文件,修改为下面2行 ErrorLog "| /usr/local/apache/bin/rotatelog ...

  4. *args和**kwargs

    #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值,**kwargs有key值 ...

  5. enumerate给列表加索引

    >>> list = ['a','b','c'] >>> for i,j in enumerate(list): print(i,j) 0 a 1 b 2 c &g ...

  6. ST-LINK V2 DIY笔记(一)

    最近一段时间调试STM32板子的时候,都是用JLINK+杜邦线,或者拿官方板子当STLINK用,可以用,但是体积比较大,有时候觉得比较麻烦.正好前一阵手头项目少,就想DIY一个STLINK. 图是网上 ...

  7. 使用supervisor管理进程

    Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...

  8. Java基础学习(四)-- 接口、集合框架、Collection、泛型详解

    接口 一.接口的基本概念 关键字为:Interface,在JAVA编程语言中是一个抽象类型,是抽象方法的集合.也是使用.java文件编写.   二.接口声明 命名规范:与类名的命名规范相同,通常情况下 ...

  9. jqurey datatables属性

    $('selector').dataTable( { /* * 默认为true * 是否自动计算列宽,计算列宽会花费一些时间,如果列宽通过aoColumns传递,可以关闭该属性作为优化 */ &quo ...

  10. 更换yum源

    1. 首先备份 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2. 使用阿里云的 ...