- 论坛徽章:
- 0
|
本帖最后由 bintre 于 2010-08-04 14:43 编辑
- #!/usr/bin/perl
- use strict;
- use warnings;
- use threads;
- use threads::shared;
- use Thread::Queue;
- use File::Find;
- use Benchmark;
- use File::Copy;
- my ($time_start,$time_end);
- $time_start=new Benchmark;
- my $infile = new Thread::Queue;
- my $MAX_THREADS = 10;
- for (my $n = 0; $n < $MAX_THREADS; $n++)
- {
- threads->create(\&thread_copy);
- }
- find(\&wanted,"./");
- sub wanted {
- my $file;
- $file=$File::Find::name;
- $infile->enqueue($file);
- if($infile->pending() > $MAX_THREADS * 5){
- sleep 1;
-
- }
-
- }
- while($infile->pending() > 0){
- sleep 1;
- }
- $time_end=new Benchmark;
- my $usetm=timediff($time_end, $time_start);
- print "use time: ", timestr($usetm), "\n";
- print "Press Enter to exit ...";
- <STDIN>;
- foreach my $thread (threads->list(threads::all))
- {
- $thread->detach();
- }
- sub thread_copy{
- while( my $file=$infile->dequeue()){
- print "$file\n";
- File::Copy::copy($file,"~/tmp");
- }
- }
复制代码 文件太多了。整个多线程的。各位看看有缺陷吗? |
|