Thread 类

(PECL pthreads >= 2.0.0)

简介

当调用 Thread 对象的 start 方法时,该对象的 run 方法中的代码将在独立线程中并行执行。

run 方法中的代码执行完毕之后,独立线程立即退出,并且等待合适的时机由创建者线程加入(join)。

Warning

依赖于引擎本身的机制检测何时加入线程可能引发非预期的行为,程序员应该尽可能的显式控制线程加入的时机。

类摘要

Thread extends Threaded implements Countable , Traversable , ArrayAccess {
/* 方法 */
public detach ( void ) : void
public getCreatorId ( void ) : int
public static getCurrentThread ( void ) : Thread
public static getCurrentThreadId ( void ) : int
public getThreadId ( void ) : int
public static globally ( void ) : mixed
public isJoined ( void ) : bool
public isStarted ( void ) : bool
public join ( void ) : bool
public kill ( void ) : void
public start ([ int $options ] ) : bool
/* 继承的方法 */
public Threaded::chunk ( int $size , bool $preserve ) : array
public Threaded::count ( void ) : int
public Threaded::extend ( string $class ) : bool
public Threaded::from ( Closure $run [, Closure $construct [, array $args ]] ) : Threaded
public Threaded::getTerminationInfo ( void ) : array
public Threaded::isRunning ( void ) : bool
public Threaded::isTerminated ( void ) : bool
public Threaded::isWaiting ( void ) : bool
public Threaded::lock ( void ) : bool
public Threaded::merge ( mixed $from [, bool $overwrite ] ) : bool
public Threaded::notify ( void ) : bool
public Threaded::notifyOne ( void ) : bool
public Threaded::pop ( void ) : bool
public Threaded::run ( void ) : void
public Threaded::shift ( void ) : boolean
public Threaded::synchronized ( Closure $block [, mixed $... ] ) : mixed
public Threaded::unlock ( void ) : bool
public Threaded::wait ([ int $timeout ] ) : bool
}

Table of Contents

User Contributed Notes

german dot bernhardt at gmail dot com 27-Mar-2016 11:11
<?php
# ERROR GLOBAL VARIABLES IMPORT

$tester=true;

function
tester(){
 global
$tester;
 
var_dump($tester);
}

tester(); // PRINT -> bool(true)

class test extends Thread{
 public function
run(){
  global
$tester;
 
tester(); // PRINT -> NULL
 
}
}
$workers=new test();
$workers->start();

?>
312036773 at qq dot com 27-Dec-2015 04:38
<?php
class STD extends Thread{
    public function
put(){
       
       
$this->synchronized(function(){
            for(
$i=0;$i<7;$i++){

   
printf("%d\n",$i);
   
$this->notify();
    if(
$i < 6)
   
$this->wait();
    else
        exit();
   
sleep(1);
}
        });

    }

        public function
flush(){
       
$this->synchronized(function(){
            for(
$i=0;$i<7;$i++){
   
flush();
   
$this->notify();
    if(
$i < 6)
   
$this->wait();
    else
        exit();
    }
});
}
}

class
A extends Thread{
    private
$std;
    public function
__construct($std){
       
$this->std = $std;
    }
    public function
run(){
       
$this->std->put();
    }
}

class
B extends Thread{
    private
$std;
    public function
__construct($std){
       
$this->std = $std;
    }
    public function
run(){
       
$this->std->flush();
    }
}
ob_end_clean();
echo
str_repeat(" ", 1024);
$std = new STD();
$ta = new A($std);
$tb = new B($std);
$ta->start();
$tb->start();
german dot bernhardt at gmail dot com 01-Apr-2014 11:01
<?php

class workerThread extends Thread {
 public function
__construct($i){
 
$this->i=$i;
 }

 public function
run(){
  while(
true){
   echo
$this->i;
  
sleep(1);
  }
 }
}

for(
$i=0;$i<50;$i++){
 
$workers[$i]=new workerThread($i);
 
$workers[$i]->start();
}

?>