PHP使用Pthread实现的多线程操作实例

5年以前  |  阅读数:535 次  |  编程语言:PHP 

本文实例讲述了PHP使用Pthread实现的多线程操作。分享给大家供大家参考,具体如下:


    <?php 
    class vote extends Thread {
      public $res  = '';
      public $url  = array();
      public $name  = '';
      public $runing = false;
      public $lc   = false;
      public function __construct($name) {
        $this->res  = '暂无,第一次运行.';
        $this->param  = 0;
        $this->lurl  = 0;
        $this->name  = $name;
        $this->runing = true;
        $this->lc   = false;
      }
      public function run() {
        while ($this->runing) {
          if ($this->param != 0) {
            $nt     = rand(1, 10);
            echo "线程[{$this->name}]收到任务参数::{$this->param},需要{$nt}秒处理数据.\n";
            $this->res  = rand(100, 999);
            sleep($nt);
            $this->lurl = $this->param;
            $this->param  = '';
          } else {
            echo "线程[{$this->name}]等待任务..\n";
          }
          sleep(1);
        }
      }
    }
    //这里创建线程池.
    $pool[] = new vote('a');
    $pool[] = new vote('b');
    $pool[] = new vote('c');
    //启动所有线程,使其处于工作状态
    foreach ($pool as $w) {
      $w->start();
    }
    //派发任务给线程
    for ($i = 1; $i < 10; $i++) {
      $worker_content = rand(10, 99);
      while (true) {
        foreach ($pool as $worker) {
          //参数为空则说明线程空闲
          if ($worker->param=='') {
            $worker->param = $worker_content;
            echo "[{$worker->name}]线程空闲,放入参数{$worker_content},上次参数[{$worker->lurl}]结果[{$worker->res}].\n";
            break 2;
          }
        }
        sleep(1);
      }
    }
    echo "所有线程派发完毕,等待执行完成.\n";
    //等待所有线程运行结束
    while (count($pool)) {
      //遍历检查线程组运行结束
      foreach ($pool as $key => $threads) {
        if ($worker->param=='') {
          echo "[{$threads->name}]线程空闲,上次参数[{$threads->lurl}]结果[{$threads->res}].\n";
          echo "[{$threads->name}]线程运行完成,退出.\n";
          //设置结束标志
          $threads->runing = false;
          unset($pool[$key]);
        }
      }
      echo "等待中...\n";
      sleep(1);
    }
    echo "所有线程执行完毕.\n";

希望本文所述对大家php程序设计有所帮助。

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
文件下载
获取IMEI
贪吃蛇
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例