php pthread多线程应用demo:
class TestThread extends Thread{
public $text;
public function run(){
$this->text = file_get_contents("http://www.buyigang.com");
}
}
$threads = array();
$time = microtime(true);
for($i=0;$i<20;$i++){
$threads[$i] = new TestThread;
$threads[$i]->start();
}
for($i=0;$i<20;$i++){
if($threads[$i]->join()){
file_put_contents(‘a.txt‘,$i,FILE_APPEND);
}
}
echo microtime(true)-$time.‘
==========================================
‘;
$time = microtime(true);
for($i=0;$i<20;$i++){
$text = file_get_contents("http://www.buyigang.com");
file_put_contents(‘b.txt‘,$i,FILE_APPEND);
}
echo microtime(true)-$time.‘
时间: 2024-10-10 07:36:23