PERL实现多线程的一些demo程序
发布时间:2020-12-16 00:12:01 所属栏目:大数据 来源:网络整理
导读:以前学perl时候的一些笔记。主要是perl来写多线程的程序,整理一下 1 #!/bin/perluse threads;use threads::shared; # 涉及到一些进程间变量的共享,用这个模块 my $process = 4;my $child_num = 0; print $threads::VERSION.chr(10); while (1) { if ($chil
|
以前学perl时候的一些笔记。主要是perl来写多线程的程序,整理一下 1 #!/bin/perl
use threads;
use threads::shared; # 涉及到一些进程间变量的共享,用这个模块
my $process = 4;
my $child_num = 0;
print $threads::VERSION.chr(10);
while (1) {
if ($child_num < $process){
my $params = $child_num.':..........';
my $thr = threads->create(&;start_thread,$params);
$child_num ++;
}
#foreach my $t(threads->list()){
foreach my $t(threads->list(threads::joinable)){
$t->join();
}
# all tasks done and no running child,then exit
if ($child_num==4){
exit;
}
}
sub start_thread(){
# do actually task here
my $param = shift;
print $param.chr(10);
}
2 my $maxchild=10;
for($i=0;$i<=$maxchild-1;$i++)
{
my $child=fork();
if($child)
{ # child >; 0,so we're the parent
warn "launching child $childn";
}else{
do_child($i); # child handles
exit 0; # child is done
}
}
exit;
sub do_child
{
my $child_number=shift(@_);
print("child,$child_number n");
} 3?
use Thread;
#use threads::shared;
my @threads;
open(MHO,$mhofile);
my @mholist=<MHO>;
foreach my $mho (@mholist) {
next unless defined $mho;
print "start one thread";
$threads[$tempcount]=Thread->new(&;start_thread,$mho);
$tempcount++;
}
foreach my $thread (@threads) {
$thread->join();
}
sub start_thread{
my ($infomho)=@_;
print "in thread $infomho";
sleep 20;
}
4 #!/bin/perl
use strict;
use threads;
use Cwd;
use POSIX qw(strftime);
################################################################################
# 函数名: count
# 函数描述: 数数
# 输入: name 随意输入一个名字
# 输出: 无
# 调用:
# 被调用:
# 返回:
################################################################################
sub count
{
my ($name) = @_;
my $current_time = strftime "%Y-%m-%d %H:%M:%S",localtime;
for ($i = 0; $i <= 10000; $i++)
{
print "$current_time $name $i";
}
}
创建第一批线程
my $thread_1_01 = threads->create('count',Thread_1);
my $thread_1_02 = threads->create('count',Thread_2);
my $thread_1_03 = threads->create('count',Thread_3);
my $thread_1_04 = threads->create('count',Thread_4);
# 等待第一批线程结束完成
$thread_1_01->join();
$thread_1_02->join();
$thread_1_03->join();
$thread_1_04->join();
# 创建第二批线程
my $thread_2_01 = threads->create('count',Thread_5);
my $thread_2_02 = threads->create('count',Thread_6);
my $thread_2_03 = threads->create('count',Thread_7);
# 等待第二批线程结束完成
$thread_2_01->join();
$thread_2_02->join();
$thread_2_03->join();
参考文档: http://www.cnblogs.com/joechen/archive/2009/04/27/1444569.html http://www.cnblogs.com/joechen/archive/2009/04/27/1444602.html 5 perl语言last和next命令 #last 退出循环陈述
for($i=1;$i<=10;$i++)
{
last if ($i==5); #如果$i等于5的话就退出for循环
print"$in";
}
#会把1到4之间的数值显示出来.
#next 到循环的下一个陈述
for($i<=10;$i++)
{
#如果是2的倍数的话,就到循环的下一个陈述
next if($i%2)==0)
print"$i是一个奇数!n";
}
#会把1以10之间的奇数显示出来。
参考文档:http://hi.baidu.com/619195553dream/item/6ac361a8d4c137921510739f 总结一下,这个博客是以前做一个perl多线程时候积累的一些博客。收藏一下。^_^ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
