php thread扩展的使用

安装教程请参考thread安装

直接上一段简单的代码

 
class test extends Thread {

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

  public function run() {
    $param=$this->fun;
    $this->$param();
  }

  private function test1()
  {
    echo 1;
    echo "
"
; } private function test2() { sleep(3); echo 2; echo "
"
; } private function test3() { echo 3; echo "
"
; } } $arr=array('test1','test2','test3'); foreach ($arr as $fun) { $th[]=new test($fun); } foreach ($th as $worker) { $worker->start(); }

输出结果是
1
3
2

test 类继承thread类,需要实现run方法,start方法会自动开启一个线程来执行run方法里的程序,thread具体的方法请参考官方手册
http://php.net/manual/zh/class.thread.php

你可能感兴趣的:(php thread扩展的使用)