文字

Thread 类

(PECL pthreads >= 2.0.0)

简介

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

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

Warning

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

类摘要

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

Table of Contents

  • Thread::detach — 执行
  • Thread::getCreatorId — 识别
  • Thread::getCurrentThread — 识别
  • Thread::getCurrentThreadId — 识别
  • Thread::getThreadId — 识别
  • Thread::globally — 执行
  • Thread::isJoined — 状态监测
  • Thread::isStarted — 状态检测
  • Thread::join — 同步
  • Thread::kill — 执行
  • Thread::start — 执行

用户评论:

[#1] 66377393 at qq dot com [2015-01-14 09:30:26]

<?php
class demo extends Thread{
    public $num = 0;
    public function __construct($num){
        $this->num = $num+200;
    }

    public function run(){
        //?????start???????run????У??????????????run?????
        return $this->num;
    }
}

//???????
$t = microtime(true);
for($i = 0 ;$i < 10 ; $i++){
    //????????
    $pool[] = new demo($i);
}

foreach($pool as $work){
    //????????????? run ????
    $work->start();
}

foreach($pool as $key=>$value){
    //???????????????
    while($value->isRunning()){
        usleep(10);
    }
    //??????????????????????????????
    if($value->join()){
        $row[$key] = $value->num;
    }
}
//?????????
$e = microtime(true);

var_dump($row);
echo '<br />';
echo ($e-$t);

[#2] german dot bernhardt at gmail dot com [2014-04-01 23:01:23]

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

?>

[#3] hanscastroadmn at gmail dot com [2014-01-14 11:00:31]



class FuerzaBruta extends Thread
{
    private $n;
    private $out;
    private $smap;
    
    public function FuerzaBruta($numero)
    {
        $this->n = $numero; // Numero de caracteres
        
        $this->smap = ['a','b','c','d'];
        
        $this->start();
    }
    
    public function run(){
        for($i = 0; $i < pow(count($this->smap), $this->n) - 1; $i++){
           $this->visualizar();
           $this->siguiente();
        }
        
        $this->visualizar();
    }
    
    public function siguiente(){
        $this->out[0]++;
        $m = 0;
        while($this->out[$m] == count($this->smap)){
            $this->out[$m++] = 0;
            $this->out[$m]++;
        }
    }
    
    public function visualizar(){
        $output = "";
        for($i = 0;$i < $this->n; $i++){
            $output += $this->smap[$this->out[$i]];
        }
        
        echo $output;
    }
    
}

//Genera todas las combinaciones de 5 caracteres.
for($i = 1; $i < 5; $i++) new FuerzaBruta($i);

// Alg??n error tendr??...

[#4] actrace at icloud dot com [2013-12-06 07:34:58]

<?php

class vote extends Thread {

    public 
$res    '';
    public 
$url    = array();
    public 
$name   '';
    public 
$runing false;
    public 
$lc     false;

    public function 
__construct($name) {

        
$this->res    '????,?????????.';
        
$this->param    0;
        
$this->lurl   0;
        
$this->name   $name;
        
$this->runing true;
        
$this->lc     false;
    }

    public function 
run() {
        while (
$this->runing) {

            if (
$this->param != 0) {
                
$nt          rand(110);
                echo 
"???[{$this->name}]??????????::{$this->param},???{$nt}????????.\n";
                
$this->res   rand(100999);
                
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(1099);
    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";
?>


???????????????????,??????????????????????????????,????????.

上一篇: 下一篇: