一. 线程的创建和使用

1. thread类

基本的创建和使用:

<?php

//通过继承thread类来实现自己的线程类mythread

class mythread extends thread{

  //重写构造函数

  function __construct(){

  }

  //重写run方法(运行的是子线程需要执行的任务)

  function run(){

  }

}

//对象的实例化和运行就和java一样

$mt = new mythread();

$mt->start();

当然,作为线程类,必须还有另外一些用于查询线程状态以及管理线程的方法

<?php

//获取创建线程的父线程id

thread::getcreatorid 

//获取当前线程id

thread::getcurrentthreadid

//获取当前线程引用

thread::getcurrentthread 

//将线程加入检测

thread::join

//查看线程是否被检测(是否被join)

thread::isjoined

//强行杀死线程

thread::kill

2.worker类

worker类的父类是thread类,因此基本用法和thread一样。而worker类相对于thread类来说,增加了线程复用的功能(以降低创建销毁线程所耗费的资源),通常与stackable类连用,也就是说worker类既可以当做线程使用,也可以当做任务的容器来使用,如:

<?php

class task extends stackable{

  function __construct($no){

    $this->no = $no;

  }

  function run(){

    echo "task{$this->no}:run".php_eol;

  }

}

class mywork extends worker{

  function __construct(){

  }

  function run(){

  }

}

$t1= new task(1);

$t2= new task(2);

$t3= new task(3);

$my = new mywork();

$my->stack($t1);

$my->stack($t2);

$my->start();

$my->stack($t3);

最终输出:

task1:run

task2:run

task3:run

当然worker类还有其他一些方法来用于父线程对其进行管理

//获取还没执行的任务数量

worker::getstacked 

//判断worker是否关闭

worker::isshutdown

//判断worker是否在工作

worker::isworking

//关闭销毁worker

worker::shutdown

//将任务压栈

worker::stack

//将任务出栈(该api有问题,慎用)

worker::unstack

二. php线程遇到的一些问题与注意点

1.线程类的属性不能直接进行哈希表(数组)操作,如:

//这样是无效的

$this->var1["hello"] = "world"; 

//改为

$this->var1 = ["hello"=>"world"];

为什么?因为线程类属性的赋值是通过序列化实现的,其本质是存储了序列化数据,因此不支持php常用直接操作哈希表(数组)的操作。

2.线程类的属性不能是“闭包函数”

原因:闭包函数不能序列化;因此,如果想在线程里用“回调函数”的话,那就放弃线程吧;

3.线程对象开辟了php的第二空间

(1)线程在创建之后,无法访问到父线程的变量,诸如$globals或global等用法都无法操作父线程的全局变量,这应该是考虑到了线程安全的问题;

(2)但是父线程却能够访问子线程对象的内容;

扩展内容

php pthread 多线程

线程,有时称为轻量级进程,是程序执行的最小单元。线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,它与同属一个进程的其它线程共享进程所拥有的全部资源。一个线程可以创建和撤消另一个线程,同一进程中的多个线程之间可以并发执行。每一个程序都至少有一个线程,那就是程序本身,通常称为主线程。线程是程序中一个单一的顺序控制流程。 在单个程序中同时运行多个线程完成不同的工作,称为多线程。

<?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();
}
?>

我们把上述代码修改一下,看看效果

<?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";
?>

我们直接调用start方法,而没有调用join。主线程不会等待,而是在输出main thread。子线程等待3秒才输出hello world。