©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PECL pthreads >= 2.0.0)
当调用 Thread 对象的 start 方法时,该对象的 run 方法中的代码将在独立线程中异步执行。
run 方法中的代码执行完毕之后,独立线程立即退出,并且等待合适的时机由创建者线程加入(join)。
依赖于引擎本身的机制检测何时加入线程可能引发非预期的行为,程序员应该尽可能的显式控制线程加入的时机。
$options
] )$size
, boolean $preserve
)$class
)$run
[, Closure $construct
[, array $args
]] )$from
[, mixed $overwrite
] )$block
[, mixed $...
] )$timeout
] )[#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(1, 10);
echo "???[{$this->name}]??????????::{$this->param},???{$nt}????????.\n";
$this->res = rand(100, 999);
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(10, 99);
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";
?>
???????????????????,??????????????????????????????,????????.