7
777
Unregistered / Unconfirmed
GUEST, unregistred user!
我用多线程对一表进行检索,开始只用一个线程如果表中记录数大于500则
加一线程,大于1000再加一线程,最多开1000个线程。反之记录数小于1000
减一线程,小于500只用一个线程。
不知怎样动态增加减少线程???并在减少线程时释放内存???
我是动态创建的query,如果开多个线程用一执行函数,一个query可以吗??
还是需每个线程用一个query???
我的程序是从表中读用户email信息,并接收邮件。
小弟没用过多线程,请各位高手指点!!!
原码如下,
unit button;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SakMsg, SakPOP3, Db, DBTables, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
q: TQuery;
base: TDatabase;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses pop3th;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
t.Create(false);
end;
end.
多线程单元:
unit pop3th;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SakMsg, SakPOP3, Db, DBTables;
type
t = class(TThread)
private
pop:Tsakpop;
list:Tsakmsglist;
query:Tquery;
query1:Tquery;
protected
procedure Execute; override;
end;
implementation
procedure t.Execute;
var
y:integer;//无限循环变量
new:integer; //新邮件数
mailcount:integer;//邮件总数
c:integer;//mailcount-new的值
s:integer;//新邮件数循环变量
i:integer;//1 到记录总数循环变量
bit:string;//表popbit字段值
msg:Tsakmsg;
userid,from1,subject1,date1,text1,file1:string;//输出参数
user,mail:string;//mail为email地址 , user为email用户名
p:integer; //取@前的用户名
po:integer;//取日期格式
str,s1,s2,s3,s4:string;//取日期格式
begin
pop:=Tsakpop.Create(nil);
list:=Tsakmsglist.Create(nil);
query:=Tquery.Create(nil);
query1:=Tquery.Create(nil);
query1.DatabaseName:='delphi';
query.DatabaseName:='delphi';
//无限循环
y:=1;
while y<2 do
with query1 do
begin
close;
sql.Clear ;
sql.text:='select * from popinput';
open;
end;
for i:=1 to query1.RecordCount do
begin
bit:=query1.fieldbyname('popbit').asstring;
mail:=query1.fieldbyname('email').asstring;
userid:=query1.fieldbyname('userid').asstring;
if bit='1' then
begin
with query do
begin
close;
sql.Clear ;
sql.text:='update popinput set popbit=0 where email='+''''+mail+'''';
execsql;
end;
user:=mail;
p:=pos('@',user);
user:=copy(user,1,p-1);
try
pop.Host:=query1.fieldbyname('pophost').asstring;
pop.UserId:=user;
pop.UserPasswd:=query1.fieldbyname('poppw').asstring;
pop.Connect ;
pop.Login ;
pop.Init ;
new:=pop.NewMsgsCount ;
mailcount:=pop.MsgsCount ;
pop.RetrieveAllMessages(list);
pop.Quit ;
c:=mailcount-new;
if new>0 then
begin
for s:=c to mailcount-1 do
begin
msg:=list.Items[c];
from1:=msg.From;
subject1:=msg.Subject ;
str:=msg.Date ;
text1:=msg.Text.Text ;
file1:='';
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
po:=pos(' ',str);
s1:=copy(str,1,po-1);
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
s2:=copy(str,1,3);
if s2='Jan' then
s2:=inttostr(1);
if s2='Feb' then
s2:=inttostr(2);
if s2='Mar' then
s2:=inttostr(3);
if s2='Apr' then
s2:=inttostr(4);
if s2='May' then
s2:=inttostr(5);
if s2='Jun' then
s2:=inttostr(6);
if s2='Jul' then
s2:=inttostr(7);
if s2='Aug' then
s2:=inttostr(8);
if s2='Sep' then
s2:=inttostr(9);
if s2='Oct' then
s2:=inttostr(10);
if s2='Nov' then
s2:=inttostr(11);
if s2='Dec' then
s2:=inttostr(12);
str:=copy(str,5,length(str)-4);
s3:=copy(str,1,4);
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
po:=pos(' ',str);
s4:=copy(str,1,po-1);
str:=s3+'/'+s2+'/'+s1+'/'+s4;
date1:=str;
with query do
begin
close;
sql.clear;
sql.Text:='insert into popoutput values(';
sql.add(''''+userid+''''+',');
if from1<>'' then
sql.Add(''''+from1+''''+',')
else
sql.Add('null,');
if subject1<>'' then
sql.add(''''+subject1+''''+',')
else
sql.add('null,');
if date1<>'' then
sql.add(''''+date1+''''+',')
else
sql.add('null,');
if text1<>'' then
sql.add(''''+text1+''''+',')
else
sql.add('null,');
if file1<>'' then
sql.add(''''+file1+'''')
else
sql.add('null');
sql.add(')');
execsql;
end;
c:=c+1;
end;
end;
query1.Next ;
except
query1.Next ;
end;
with query do
begin
close;
sql.Clear ;
sql.text:='update popinput set popbit=1 where email='+''''+mail+'''';
execsql;
end;
//query1.Next ;
end
else
query1.Next ;
end;
end;//无限循环
end;
end.
加一线程,大于1000再加一线程,最多开1000个线程。反之记录数小于1000
减一线程,小于500只用一个线程。
不知怎样动态增加减少线程???并在减少线程时释放内存???
我是动态创建的query,如果开多个线程用一执行函数,一个query可以吗??
还是需每个线程用一个query???
我的程序是从表中读用户email信息,并接收邮件。
小弟没用过多线程,请各位高手指点!!!
原码如下,
unit button;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SakMsg, SakPOP3, Db, DBTables, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
q: TQuery;
base: TDatabase;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses pop3th;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
t.Create(false);
end;
end.
多线程单元:
unit pop3th;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SakMsg, SakPOP3, Db, DBTables;
type
t = class(TThread)
private
pop:Tsakpop;
list:Tsakmsglist;
query:Tquery;
query1:Tquery;
protected
procedure Execute; override;
end;
implementation
procedure t.Execute;
var
y:integer;//无限循环变量
new:integer; //新邮件数
mailcount:integer;//邮件总数
c:integer;//mailcount-new的值
s:integer;//新邮件数循环变量
i:integer;//1 到记录总数循环变量
bit:string;//表popbit字段值
msg:Tsakmsg;
userid,from1,subject1,date1,text1,file1:string;//输出参数
user,mail:string;//mail为email地址 , user为email用户名
p:integer; //取@前的用户名
po:integer;//取日期格式
str,s1,s2,s3,s4:string;//取日期格式
begin
pop:=Tsakpop.Create(nil);
list:=Tsakmsglist.Create(nil);
query:=Tquery.Create(nil);
query1:=Tquery.Create(nil);
query1.DatabaseName:='delphi';
query.DatabaseName:='delphi';
//无限循环
y:=1;
while y<2 do
with query1 do
begin
close;
sql.Clear ;
sql.text:='select * from popinput';
open;
end;
for i:=1 to query1.RecordCount do
begin
bit:=query1.fieldbyname('popbit').asstring;
mail:=query1.fieldbyname('email').asstring;
userid:=query1.fieldbyname('userid').asstring;
if bit='1' then
begin
with query do
begin
close;
sql.Clear ;
sql.text:='update popinput set popbit=0 where email='+''''+mail+'''';
execsql;
end;
user:=mail;
p:=pos('@',user);
user:=copy(user,1,p-1);
try
pop.Host:=query1.fieldbyname('pophost').asstring;
pop.UserId:=user;
pop.UserPasswd:=query1.fieldbyname('poppw').asstring;
pop.Connect ;
pop.Login ;
pop.Init ;
new:=pop.NewMsgsCount ;
mailcount:=pop.MsgsCount ;
pop.RetrieveAllMessages(list);
pop.Quit ;
c:=mailcount-new;
if new>0 then
begin
for s:=c to mailcount-1 do
begin
msg:=list.Items[c];
from1:=msg.From;
subject1:=msg.Subject ;
str:=msg.Date ;
text1:=msg.Text.Text ;
file1:='';
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
po:=pos(' ',str);
s1:=copy(str,1,po-1);
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
s2:=copy(str,1,3);
if s2='Jan' then
s2:=inttostr(1);
if s2='Feb' then
s2:=inttostr(2);
if s2='Mar' then
s2:=inttostr(3);
if s2='Apr' then
s2:=inttostr(4);
if s2='May' then
s2:=inttostr(5);
if s2='Jun' then
s2:=inttostr(6);
if s2='Jul' then
s2:=inttostr(7);
if s2='Aug' then
s2:=inttostr(8);
if s2='Sep' then
s2:=inttostr(9);
if s2='Oct' then
s2:=inttostr(10);
if s2='Nov' then
s2:=inttostr(11);
if s2='Dec' then
s2:=inttostr(12);
str:=copy(str,5,length(str)-4);
s3:=copy(str,1,4);
po:=pos(' ',str);
str:=copy(str,po+1,length(str)-po);
po:=pos(' ',str);
s4:=copy(str,1,po-1);
str:=s3+'/'+s2+'/'+s1+'/'+s4;
date1:=str;
with query do
begin
close;
sql.clear;
sql.Text:='insert into popoutput values(';
sql.add(''''+userid+''''+',');
if from1<>'' then
sql.Add(''''+from1+''''+',')
else
sql.Add('null,');
if subject1<>'' then
sql.add(''''+subject1+''''+',')
else
sql.add('null,');
if date1<>'' then
sql.add(''''+date1+''''+',')
else
sql.add('null,');
if text1<>'' then
sql.add(''''+text1+''''+',')
else
sql.add('null,');
if file1<>'' then
sql.add(''''+file1+'''')
else
sql.add('null');
sql.add(')');
execsql;
end;
c:=c+1;
end;
end;
query1.Next ;
except
query1.Next ;
end;
with query do
begin
close;
sql.Clear ;
sql.text:='update popinput set popbit=1 where email='+''''+mail+'''';
execsql;
end;
//query1.Next ;
end
else
query1.Next ;
end;
end;//无限循环
end;
end.