略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Thread

2024-04-19

Thread 类

(PECL pthreads >= 2.0.0)

简介

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

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

警告

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

类摘要

class Thread extends Threaded implements Countable, Traversable, ArrayAccess {
/* 方法 */
public getCreatorId(): int
public static getCurrentThread(): Thread
public static getCurrentThreadId(): int
public getThreadId(): int
publicisJoined(): bool
publicisStarted(): bool
publicjoin(): bool
public start(int $options = ?): bool
/* 继承的方法 */
public Threaded::chunk(int $size, bool $preserve): array
public Threaded::count(): int
public Threaded::extend(string $class): bool
public Threaded::isRunning(): bool
public Threaded::isTerminated(): bool
publicThreaded::merge(mixed $from, bool $overwrite = ?): bool
public Threaded::notify(): bool
public Threaded::notifyOne(): bool
public Threaded::pop(): bool
public Threaded::run(): void
public Threaded::shift(): boolean
public Threaded::synchronized(Closure $block, mixed $... = ?): mixed
public Threaded::wait(int $timeout = ?): bool
}

目录

add a noteadd a note

User Contributed Notes 2 notes

up
16
german dot bernhardt at gmail dot com
8 years ago
<?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();
}

?>
up
4
german dot bernhardt at gmail dot com
6 years ago
<?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();

?>

官方地址:https://www.php.net/manual/en/class.thread.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3