2024年PHP日本会议

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条注释

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

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