## 多线程编程(18) - 再从一个小例子出发 转

涂孟超

``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

var
h1,h2,h3: THandle;

{第一个线程的入口函数: 画红色椭圆}
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox1.Canvas.Brush.Color := clRed;
for i := 0 to 50000 do with Form1.PaintBox1 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;

{第二个线程的入口函数: 画绿色椭圆}
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox2.Canvas.Brush.Color := clGreen;
for i := 0 to 50000 do with Form1.PaintBox2 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;

{第三个线程的入口函数: 画蓝色椭圆}
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox3.Canvas.Brush.Color := clBlue;
for i := 0 to 50000 do with Form1.PaintBox3 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
begin
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(h1);
CloseHandle(h2);
CloseHandle(h3);
end;

end.

``````

``````object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 206
ClientWidth = 371
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object PaintBox1: TPaintBox
Left = 8
Top = 8
Width = 114
Height = 153
end
object PaintBox2: TPaintBox
Left = 128
Top = 8
Width = 114
Height = 153
end
object PaintBox3: TPaintBox
Left = 248
Top = 8
Width = 114
Height = 153
end
object Button1: TButton
Left = 288
Top = 172
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end

``````

``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

var
h1,h2,h3: THandle;

{线程的入口函数: 不同的线程画不同颜色的椭圆}
var
i,x1,y1,x2,y2: Integer;
paint: TPaintBox;
begin
case Integer(p) of
1: begin
paint := Form1.PaintBox1;
paint.Canvas.Brush.Color := clRed;
end;
2: begin
paint := Form1.PaintBox2;
paint.Canvas.Brush.Color := clGreen
end;
3: begin
paint := Form1.PaintBox3;
paint.Canvas.Brush.Color := clBlue;
end;
end;

for i := 0 to 5000 do with paint do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
begin
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(h1);
CloseHandle(h2);
CloseHandle(h3);
end;

end.

``````

``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

const
colors: array[0..2] of TColor = (clRed, clGreen, clBlue);
var
hArr: array[0..2] of THandle;
panitArr: array[0..2] of TPaintBox;

var
i,n,x1,y1,x2,y2: Integer;
begin
n := Integer(p);
panitArr[n].Color := colors[n];

for i := 0 to 5000 do with panitArr[n] do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
i: Integer;
begin
panitArr[0] := PaintBox1;
panitArr[1] := PaintBox2;
panitArr[2] := PaintBox3;
for i := 0 to Length(hArr) - 1 do
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Length(hArr) - 1 do CloseHandle(hArr[i]);
end;

end.

``````

### 涂孟超

python浓缩（18）多线程编程

2016/04/25
74
0
java学习计划

Bony
2016/05/14
64
0
Python核心编程：第18章 多线程编程

18.1 引言/动机 1.多线程编程对于某些任务是最理想的。这些任务具有以下特点：它们本质就是异步的，需要有多个并发事务，各个事务的运行顺序可以是不确定的、随机的、 不可预测的。这样的编程...

finndai
2016/12/08
49
0

JR 是一种专为解决并行编程问题的编程语言。JR 是在 Java 语言的基础上增加了并行编程（例如进程和信号量等）概念的一个新的语言。 JR 经常在学校中用来教授并行编程课程。 在本文中我们主要...

2010/06/28
1K
3
Linux多线程并发服务器编程（线程池，FTP服务器）

2018/06/26
0
0

Nginx 快速安装详解

13分钟前
4
0

15分钟前
3
0
nginx配置http访问自动跳转到https

server {listen 80;server_name www.域名.com;rewrite ^(.*) https://\$server_name\$1 permanent;}server {listen 443;server_name www.域名.com;root /home/www;ssl on;......

15分钟前
2
0

Excel 作为一款深受用户喜爱的电子表格工具，借助其直观的界面、出色的计算性能、数据分析和图表，已经成为数据统计领域不可或缺的软件之一。 基于Excel对数据处理与分析的卓越表现，把Excel...

15分钟前
2
0

class WeatherApp : App(WeatherView::class)class WeatherView : View("十五天天气预报") { val weatherVM: WeatherViewModel by inject() val controller: WeatherController by......

oschina4cyy
19分钟前
3
1