标签:sleep xtend 系统 独立 echo 异步执行 需要 tar 子线程
我们可以通过安装Pthread扩展来让PHP支持多线程。
<?php
//实现多线程必须继承Thread类
class test extends Thread {
    public function __construct($arg){
        $this->arg = $arg;
    }
    //当调用start方法时,该对象的run方法中的代码将在独立线程中异步执行。
    public function run(){
        if($this->arg){
            printf("Hello %s\n", $this->arg);
        }
    }
}
$thread = new test("World");
if($thread->start()) {
    //join方法的作用是让当前主线程等待该线程执行完毕
    //确认被join的线程执行结束,和线程执行顺序没关系。
    //也就是当主线程需要子线程的处理结果,主线程需要等待子线程执行完毕
    //拿到子线程的结果,然后处理后续代码。
    $thread->join();
}
?>
我们把上述代码修改一下,看看效果
<?php
class test extends Thread {
    public function __construct($arg){
        $this->arg = $arg;
    }
    public function run(){
        if($this->arg){
            sleep(3);
            printf("Hello %s\n", $this->arg);
        }
    }
}
$thread = new test("World");
$thread->start();
echo "main thread\r\n";
?>
<?php
class test extends Thread {
    private $name = ‘‘;
    private $res = null;
    public function __construct($name, $res){
        $this->name = $name;
        $this->res = $res;
    }
    public function run(){
        while(!feof($this->res)) {
            if(flock($this->res, LOCK_EX)) {
                $data = fgets($this->res);
                $data = trim($data);
                echo "Thread {$this->name} Read {$data} \r\n";
                sleep(1);
                flock($this->res, LOCK_UN);
            }
        }
    }
}
$fp = fopen(‘./test.log‘, ‘rb‘);
$threads[] = new test(‘a‘, $fp);
$threads[] = new test(‘b‘, $fp);
foreach($threads as $thread) {
    $thread->start();
}
foreach($threads as $thread) {
    $thread->join();
}
?>
111111 222222 333333 444444 555555 666666

<?php
class Total extends Thread {
    public $name = ‘‘;
    private $total = 0;
    private $startNum = 0;
    private $endNum = 0;
    public function __construct($name, $startNum, $endNum){
        $this->name = $name;
        $this->startNum = $startNum;
        $this->endNum = $endNum;
    }
    public function run(){
        for($ix = $this->startNum; $ix < $this->endNum; ++$ix) {
            $this->total += $ix;
        }
        echo "Thread {$this->name} total: {$this->total} \r\n";
    }
    public function getTotal() {
        return $this->total;
    }
}
$num = 10000000;
$threadNum = 10;
$setp = $num / $threadNum;
$startNum = 0;
$startTime = microtime(true);
for($ix = 0; $ix < $threadNum; ++$ix) {
    $endNum = $startNum + $setp;
    $thread = new Total($ix, $startNum, $endNum);
    $thread->start();
    $startNum = $endNum;
    $threads[] = $thread;
}
$total = 0;
foreach($threads as $thread) {
    $thread->join();
    $total += $thread->getTotal();
}
$endTime = microtime(true);
$time = $endTime - $startTime;
echo "total : {$total} time : {$time} \r\n";

<?php
$total = 0;
$startTime = microtime(true);
for($ix = 0; $ix < 10000000; ++$ix) {
    $total += $ix;
}
$endTime = microtime(true);
$time = $endTime - $startTime;
echo "total : {$total} time : {$time} \r\n";

我们可以看到使用多线程和不使用,得到的结果是一样的,但是处理时间,多线程就慢很多。(*主要是线程的创建也是需要资源的,而且线程之间的相互切换也需要时间,这里的例子主要说明如何把一个问题分配给多个子线程去处理,然后主线程拿到子线程的结果并处理得到我们需要的结果。)
标签:sleep xtend 系统 独立 echo 异步执行 需要 tar 子线程
原文地址:http://www.cnblogs.com/jkko123/p/6351604.html