富贵资源网 Design By www.hznty.com
本文实例讲述了PHP使用Pthread实现的多线程操作。分享给大家供大家参考,具体如下:
<"线程[{$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程序设计有所帮助。
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...