Thread 类

(PECL pthreads >= 2.0.0)

介绍

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

run 方法执行完毕后,线程会立即退出,并在适当的时候与创建的线程进行连接。

警告

依赖引擎来决定何时应该连接线程可能会导致不可预期的行为;开发者应尽可能明确地进行连接。

类概要

class Thread extends Threaded implements Countable, Traversable, ArrayAccess {
/* 方法 */
public getCreatorId(): int
public static getCurrentThread(): Thread
public static getCurrentThreadId(): int
public getThreadId(): int
public isJoined(): bool
public isStarted(): bool
public join(): bool
public start(int $options = ?): bool
/* 继承方法 */
public Threaded::chunk(int $size, bool $preserve): array
public Threaded::extend(string $class): bool
public Threaded::merge(mixed $from, bool $overwrite = ?): bool
public Threaded::synchronized(Closure $block, mixed ...$args): mixed
public Threaded::wait(int $timeout = ?): bool
}

目录

添加注释

用户贡献的注释 2 个注释

8
german dot bernhardt at gmail dot com
8 年前
<?php
# 错误全局变量导入

$tester=true;

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

tester(); // 输出 -> bool(true)

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

?>
1
german dot bernhardt at gmail dot com
10 年前
<?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();
}

?>
To Top