- 论坛徽章:
- 46
|
这里内存不是问题。稍微改一点不和谐的地方,5.20 32位strawberryperl 最多占980M- use strict;
- use warnings;
- use threads;
- use Thread::Queue;
- my $q = Thread::Queue->new();
- my %book;
- my @array=(1..1000000);
- for my $t (@array) {
- $book{$t}=$t*2;
- }
- for my $r (sort keys %book) {
- $q->enqueue($r);
- }
- $q->end();
- sub consume {
- my $i = shift;
- open my $outfile, ">>syslog$i";
- while(defined(my $r = $q->dequeue)) {
- print $outfile (localtime(time)."consume $r @_.\n");
- }
- }
- sub consume2 {
- for my $s (sort keys %book) {
- my $r = $q->dequeue;
- printf("consume $r @_.\n");
- }
- }
- my $consumer1 = threads->create(\&consume,1, "consume1");
- my $consumer2 = threads->create(\&consume,2, "consume2");
- my $consumer3 = threads->create(\&consume,3, "consume3");
- $consumer1->join();
- $consumer2->join();
- $consumer3->join();
复制代码 |
|