我们可以通过安装Pthread扩展来让PHP支持多线程。
线程,有时称为轻量级进程,是程序执行的最小单元。线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,它与同属一个进程的其它线程共享进程所拥有的全部资源。一个线程可以创建和撤消另一个线程,同一进程中的多个线程之间可以并发执行。每一个程序都至少有一个线程,那就是程序本身,通常称为主线程。线程是程序中一个单一的顺序控制流程。 在单个程序中同时运行多个线程完成不同的工作,称为多线程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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(); } ?> |
我们把上述代码修改一下,看看效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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" ; ?> |
例1如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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(); } ?> |
test.log的内容如下:
1 2 3 4 5 6 | 111111 222222 333333 444444 555555 666666 |

例2如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?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" ; |

1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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" ; |

我们可以看到使用多线程和不使用,得到的结果是一样的,但是处理时间,多线程就慢很多。(*主要是线程的创建也是需要资源的,而且线程之间的相互切换也需要时间,这里的例子主要说明如何把一个问题分配给多个子线程去处理,然后主线程拿到子线程的结果并处理得到我们需要的结果。)
版权声明:博主文章,可以不经博主允许随意转载,随意修改,知识是用来传播的。
分类:
php
标签:
php的多线程Pthread
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决