php线程

<?php


class test extends \Thread
{
    public $url;
    public $result;

    public function __construct($url)
    {
        $this->url = $url;
    }

    //实现Thread父类的run()方法
    public function run()
    {
        if ($this->url) {
            echo "*************\n";
            var_dump($this->result = model_http_curl_get($this->url));
            echo "+++++++++\n";
        }
    }
}

function model_http_curl_get($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)');
    $result = curl_exec($curl);
    curl_exec($curl);
    curl_close($curl);
//    return $result;//返回数据的话,命令行模式下东西太多不好看

    return $result ? true : false;
}


for ($i = 0; $i < 3; $i++) {
    $urls[] = 'http://www.baidu.com/s?wd=' . rand(1, 2000);
}
/* 多线程速度测试 */
$t = microtime(true);
foreach ($urls as $key => $url) {
    $workers[$key] = new test($url);
    //当调用start()方法的时候会自动执行自定义类中的run()方法(该run()方法必须在你自定义的类中实现父类Thread类中run()方法)
    $workers[$key]->start();//*******

//    if($workers[$key]->start()){
//        echo "@@@@\n";
//        echo $workers[$key]['url']."\n";
//        var_dump($workers[$key]->result);//null
//    }
}
echo "AAAAAAAAAAA";
print_r($workers);
sleep(1);
print_r($workers);
//断点--1
die;

foreach ($workers as $key => $worker) {
    while ($workers[$key]->isRunning()) {
        usleep(100);
    }

    if ($workers[$key]->join()) {
        var_dump($workers[$key]->result);
    }
}
//die;
$e = microtime(true);
echo "multiple:" . ($e - $t) . "Second<br>";

/* 单线程速度测试 */
$t = microtime(true);
foreach ($urls as $key => $url) {
    var_dump(model_http_curl_get($url));
}
$e = microtime(true);
echo "signale:" . ($e - $t) . "Second<br>";

从断点--1打印(命令行模式下运行:php demo.php),多打印几次,会发现打印结果不同:

第一次打印结果(1)如下:
*************
@@@@
http://www.baidu.com/s?wd=1451
NULL
*************
@@@@
http://www.baidu.com/s?wd=903
NULL
*************
@@@@
http://www.baidu.com/s?wd=11
NULL
AAAAAAAAAAAArray
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1451
            [result] =>
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=903
            [result] =>
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=11
            [result] =>
        )

)
bool(true)
+++++++++
bool(true)
+++++++++
Array
(
    [0] => testbool(true)
+++++++++
 Object
        (
            [url] => http://www.baidu.com/s?wd=1451
            [result] => 1
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=903
            [result] => 1
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=11
            [result] => 1
        )

)




第2次打印结果(2)如下:
*************
@@@@
http://www.baidu.com/s?wd=748
NULL
*************
@@@@
http://www.baidu.com/s?wd=910
NULL
*************
@@@@
http://www.baidu.com/s?wd=1707
NULL
AAAAAAAAAAAArray
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=748
            [result] =>
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=910
            [result] =>
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1707
            [result] =>
        )

)
bool(true)
+++++++++
Array
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=748
            [result] =>
        )

    [1] => test Object
        (
  bool(true)
+++++++++
          [url] => http://www.baidu.com/s?wd=910
            [result] =>
        )

  bool(true)
  +++++++++
[2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1707
            [result] => 1
        )

)



第3次打印结果(3)如下:
*************
@@@@
http://www.baidu.com/s?wd=646
NULL
*************
@@@@
http://www.baidu.com/s?wd=1192
NULL
*************
@@@@
http://www.baidu.com/s?wd=1519
NULL
AAAAAAAAAAAArray
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=646
            [result] =>
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1192
            [result] =>
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1519
            [result] =>
        )

)
bool(true)
+++++++++
bool(true)
+++++++++
bool(true)
+++++++++
Array
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=646
            [result] => 1
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1192
            [result] => 1
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1519
            [result] => 1
        )

)



第4次打印结果(4)如下:
*************
@@@@
http://www.baidu.com/s?wd=15
NULL
*************
@@@@
http://www.baidu.com/s?wd=1822
NULL
*************
@@@@
http://www.baidu.com/s?wd=1676
NULL
AAAAAAAAAAAArray
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=15
            [result] =>
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1822
            [result] =>
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1676
            [result] =>
        )

)
bool(true)
+++++++++
bool(true)
+++++++++
Array
(
    [0] => test Object
        (
            [url] => http://www.baidu.com/s?wd=15
            [result] =>
        )

    [1] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1822
            [result] => 1
        )

    [2] => test Object
        (
            [url] => http://www.baidu.com/s?wd=1676
            [result] => 1
        )

)
bool(true)
+++++++++

会发现一个很诡异的现象:每次打印的结果都可能不一样:

这是因为每个线程的执行流程依靠系统来调度,具体先限执行哪一个,谁也说不准。不过根据打印结果可以看出来:

1、当执行线程的start()方法时候,会自动调用run()方法,但是不会按照之前的那种单线程的思维:会一直执行run()方法(以阻塞的方式执行run()方法),他会往后继续执行,例如上面打印结果中:当sleep(1),1秒后第二次print_r($workers);的时候,才会有run()方法返回结果,说明不同的线程执行的快慢不一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值